| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The tasks in the following process guide you through using the .NET classes to upload an
object. The API provides several variations, overloads, of the
PutObject method to easily upload your data.
Uploading Objects
1 | Create an instance of the |
2 | Execute one of the |
The following C# code sample demonstrates the preceding tasks.
static AmazonS3 client;
client = Amazon.AWSClientFactory.CreateAmazonS3Client(
accessKeyID, secretAccessKeyID);
PutObjectRequest request = new PutObjectRequest();
request.WithFilePath(filePath)
.WithBucketName(bucketName)
.WithKey(keyName);
S3Response responseWithMetadata =
client.PutObject(titledRequest);Note
When uploading large objects using the .NET API, timeout might occur even while data is
being written to the request stream. You can set explicit timeout using the
PutObjectRequest object.
Example
The following C# code example uploads an object. The object data is provided as a text
string in the code. The example illustrates the use of the
AmazonS3.PutObject to upload an object. The example
uploads the object twice. In the first object upload, the
PutObjectRequest specifies only the bucket name, key name,
and sample object data. In the second object upload the
PutObjectRequest provides additional information including
the optional object metadata and a content type header. Each successive call
to AmazonS3.PutObject replaces the previous upload. For
instructions on how to create and test a working sample, see Testing the .NET Code Examples
using System;
using System.Configuration;
using System.Collections.Specialized;
using System.Net;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Util;
namespace s3.amazon.com.docsamples.createobject
{
class S3Sample
{
static string bucketName = "*** Provide bucket name ***";
static string keyName = "*** Provide key name ***";
static AmazonS3 client;
public static void Main(string[] args)
{
if (checkRequiredFields())
{
NameValueCollection appConfig =
ConfigurationManager.AppSettings;
string accessKeyID = appConfig["AWSAccessKey"];
string secretAccessKeyID = appConfig["AWSSecretKey"];
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(
accessKeyID, secretAccessKeyID))
{
Console.WriteLine("Uploading an object");
WritingAnObject();
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
static void WritingAnObject()
{
try
{
// 1. Simple object put.
PutObjectRequest request = new PutObjectRequest();
request.WithContentBody("Object data for simple put.")
.WithBucketName(bucketName)
.WithKey(keyName);
S3Response response = client.PutObject(request);
response.Dispose();
// 2. Put a more complex object with metadata and http headers.
PutObjectRequest request2 = new PutObjectRequest();
request2.WithMetaData("title", "the title")
.WithContentBody("Object data for complex put.")
//.WithFilePath(filePath)
.WithBucketName(bucketName)
.WithKey(keyName);
// Add a header to the request.
request2.AddHeaders(AmazonS3Util.CreateHeaderEntry
("ContentType", "text/xml"));
S3Response responseWithMetadata = client.PutObject(request2);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Check the provided AWS Credentials.");
Console.WriteLine(
"For service sign up go to http://aws.amazon.com/s3");
}
else
{
Console.WriteLine(
"Error occurred. Message:'{0}' when writing an object"
, amazonS3Exception.Message);
}
}
}
static bool checkRequiredFields()
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
if (string.IsNullOrEmpty(appConfig["AWSAccessKey"]))
{
Console.WriteLine(
"AWSAccessKey was not set in the App.config file.");
return false;
}
if (string.IsNullOrEmpty(appConfig["AWSSecretKey"]))
{
Console.WriteLine(
"AWSSecretKey was not set in the App.config file.");
return false;
}
if (string.IsNullOrEmpty(bucketName))
{
Console.WriteLine("The variable bucketName is not set.");
return false;
}
if (string.IsNullOrEmpty(keyName))
{
Console.WriteLine("The variable keyName is not set.");
return false;
}
return true;
}
}
}