Uploading (’Putting’) binary files to Amazon S3 in C#
So I’ve had a technology crush on S3 since it came out but I’ve only recently started to explore it. After downloading the C# S3 SOAP sample code, the first thing I tried to do was upload a jpeg. The upload was pretty easy, but upon trying to access the file via the s3 url, I only received an unintelligible string sequence. Opening it in a hex editor proved that the file didn’t get uploaded as a byte sequence, but as one long string. The reason, it turns out, was that I was using the wrong api call (duh), the call that accepts a string parameter:
public PutObjectResult put(string bucket, string key, string obj, MetadataEntry[] metadata, Grant[] accessControlList)
{
DateTime timestamp = AWSDateFormatter.GetCurrentTimeResolvedToMillis();
string signature = makeSignature(”PutObjectInline”, timestamp);
ASCIIEncoding ae = new ASCIIEncoding();
return s3.PutObjectInline(bucket, key, metadata, ae.GetBytes( obj ), obj.Length, accessControlList, StorageClass.STANDARD, false, awsAccessKeyId, timestamp, true, signature, null);
}
To use this call, I had to convert my byte sequence into a string, which was then converted to another byte sequence using an ascii encoding. That process corrupts your original byte sequence because not all bytes fit into the ascii spectrum. But where was the correct method signature? It turns out it was omitted, at least in version dated 2006-10-13. Adding the following method to AwsConnection.cs fixed my upload woes:
public PutObjectResult put(string bucket, string key, byte[] objBytes, MetadataEntry[] metadata, Grant[] accessControlList)
{
DateTime timestamp = AWSDateFormatter.GetCurrentTimeResolvedToMillis();
string signature = makeSignature(”PutObjectInline”, timestamp);
return s3.PutObjectInline(bucket, key, metadata, objBytes, objBytes.Length, accessControlList, StorageClass.STANDARD, false, awsAccessKeyId, timestamp, true, signature, null);
}

January 25th, 2008 18:42
Hi, sorry to bother you I hope you can help, basically I am having an issue, I am uploading images using c# and a handler, everything seems to be fine but when I check the url the image is blank, do you know why? any help will be appreciated.
BufferedStream bf = new BufferedStream(_FileObject.InputStream);
byte[] _FileByte = new byte[bf.Length];
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
// Create a signature for this operation
string strMySignature = S3Helper.GetSignature(
_SecretAccessKeyId,
“PutObjectInline”,
myTime);
// Create a new Access grant for anonymous users.
Grant myGrant = new Grant();
Grant[] myGrants = new Grant[1];
// Setup Access control, allow Read access to all
Group myGroup = new Group();
myGroup.URI = “http://acs.amazonaws.com/groups/global/AllUsers”;
myGrant.Grantee = myGroup;
myGrant.Permission = Permission.READ;
myGrants[0] = myGrant;
// Setup some metadata to indicate the content type
MetadataEntry myContentType = new MetadataEntry();
myContentType.Name = “Content-Type”;
myContentType.Value = _FileObject.ContentType;
MetadataEntry[] myMetaData = new MetadataEntry[1];
myMetaData[0] = myContentType;
// Finally upload the object
PutObjectResult _Result = myS3.PutObjectInline(
“images.browsner.com”,
_FileObject.FileName,
myMetaData,
_FileByte,
_FileObject.ContentLength,
myGrants,
StorageClass.STANDARD,
true,
_AWSAccessKeyId,
S3Helper.GetTimeStamp(myTime),
true,
strMySignature, null
);
February 9th, 2008 00:33
great code… It helped me alot!
please can u guide me with this too… I want to get an object and as relation to your code, I am using s3 helper and using
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
string strMySignature = S3Helper.GetSignature(
mySecretAccessKeyId, “GetObject”,
myTime);
GetObjectResult GOR = myS3.GetObject(”my_bucket123″,
“Sunset.jpg”, true, true, true, myAWSAccessKeyId, myTime, true, strMySignature,null);
and I get error like “GetObject” is not making the correct signature required to get an object… can you please help me with this, I will be very very thankfull of you