AWS SDK for .NET Documentation
PutObject Method (request)
AmazonAmazon.S3AmazonS3ClientPutObject(PutObjectRequest) Did this page help you?   Yes   No    Tell us about it...
Adds an object to an S3 bucket.
Declaration Syntax
C#
public PutObjectResponse PutObject(
	PutObjectRequest request
)
Parameters
request (PutObjectRequest)
The PutObjectRequest that defines the parameters of the operation.
Return Value
Returns a PutObjectResponse from S3.
Remarks

The response indicates that the object has been successfully stored. Amazon S3 never stores partial objects: if you receive a successful response, then you can be confident that the entire object was stored.

To ensure data is not corrupted over the network, use the Content-MD5 header. When you use the Content-MD5 header, Amazon S3 checks the object against the provided MD5 value. If they do not match, Amazon S3 returns an error. Additionally, you can calculate the MD5 while putting an object to Amazon S3 and compare the returned Etag to the calculated MD5 value.

If an object already exists in a bucket, the new object will overwrite it because Amazon S3 stores the last write request. However, Amazon S3 is a distributed system. If Amazon S3 receives multiple write requests for the same object nearly simultaneously, all of the objects might be stored, even though only one wins in the end. Amazon S3 does not provide object locking; if you need this, make sure to build it into your application layer.

If you specify a location constraint when creating a bucket, all objects added to the bucket are stored in the bucket's location.

You must have WRITE access to the bucket to add an object.

Examples

This following examples show multiple ways of creating an object.

This example shows how to put an object, with its content being passed along as a string.

CopyPutObject sample 1
// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a PutObject request
PutObjectRequest request = new PutObjectRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
    ContentBody = "This is sample content..."
};

// Put object
PutObjectResponse response = client.PutObject(request);

This example shows how to put an object, setting its content to be a file.

CopyPutObject sample 2
// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a PutObject request
PutObjectRequest request = new PutObjectRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
    FilePath = "contents.txt"
};

// Put object
PutObjectResponse response = client.PutObject(request);

This example shows how to put an object using a stream.

CopyPutObject sample 3
// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a PutObject request
PutObjectRequest request = new PutObjectRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
};
using (FileStream stream = new FileStream("contents.txt", FileMode.Open))
{
    request.InputStream = stream;

    // Put object
    PutObjectResponse response = client.PutObject(request);
}
Exceptions

Assembly: AWSSDK (Module: AWSSDK) Version: 1.5.60.0 (1.5.60.0)