我已经在这个问题上工作了很长一段时间,但没有运气。我根据http://aws.amazon.com/articles/3002109349624271使用 Amazon S3 iOS 上传 SDK
症状:
可以创建bucket,不会上传图片。 S3PutObjectResponse *putObjectResponse = [self.s3 putObject:por];
返回零。
这是我的代码: 在 viewDidLoad
self.s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
self.s3.endpoint = [AmazonEndpoints s3Endpoint:US_WEST_2];
尝试使用和不使用端点;根据https://forums.aws.amazon.com/thread.jspa?messageID=427879蝧添加
在稍后在同一视图控制器中调用的上传函数中:
...
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);
});
永远不会抛出错误,如果我转到 URL,它只会显示“指定的密钥不存在”。
如果您有任何想法,请告诉我!这真让我抓狂!