Thursday, 5 September 2013

Amazon S3 Mobile Uploading: creates bucket, won't upload image

Amazon S3 Mobile Uploading: creates bucket, won't upload image

I've been working on this problem for quite some time now with no luck. I
am using the Amazon S3 iOS upload SDK as per
http://aws.amazon.com/articles/3002109349624271
Symptoms: Can create a bucket, will not upload image. S3PutObjectResponse
*putObjectResponse = [self.s3 putObject:por]; returning nil.
Here is my code: In viewDidLoad
self.s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID
withSecretKey:SECRET_KEY];
self.s3.endpoint = [AmazonEndpoints s3Endpoint:US_WEST_2];
Tried with and without endpoint; added as per
https://forums.aws.amazon.com/thread.jspa?messageID=427879&#427879
In upload function called later in same view controller:
...
NSData *data = UIImageJPEGRepresentation(newImage, 0.9);
dispatch_queue_t queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Upload image data. Remember to set the content type.
S3PutObjectRequest *por = [[S3PutObjectRequest alloc]
initWithKey:PICTURE_NAME
inBucket:PICTURE_BUCKET];
por.contentType = @"image/jpeg";
por.data = data;
por.delegate = self;
// Put the image data into the specified s3 bucket and object.
S3PutObjectResponse *putObjectResponse = [self.s3 putObject:por];
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
if(putObjectResponse.error != nil)
{
NSLog(@"Error: %@", putObjectResponse.error);
[self showAlertMessage:[putObjectResponse.error.userInfo
objectForKey:@"message"] withTitle:@"Upload Error"];
}
});
NSLog(@"image PUT to amazon");
// Set the content type so that the browser will treat the URL as
an image.
S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides
alloc] init];
override.contentType = @"image/jpeg";
// Request a pre-signed URL to picture that has been uploaded.
S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest
alloc] init];
gpsur.key = PICTURE_NAME;
gpsur.bucket = PICTURE_BUCKET;
gpsur.expires = [NSDate
dateWithTimeIntervalSinceNow:(NSTimeInterval) 7200]; // Added an
hour's worth of seconds to the current time.
gpsur.responseHeaderOverrides = override;
// Get the URL
NSError *error;
NSURL *url = [self.s3 getPreSignedURL:gpsur error:&error];
NSString *imageURL = url.absoluteString;
NSLog(@"%@",imageURL);
});
Error is never thrown, and if I go to the URL it simply says "The
specified key does not exist."
If you have any ideas, please let me know! This is driving me crazy!

No comments:

Post a Comment