2013-02-25 135 views
9

我是AWS的新手,并将其用于iOS应用。AWS S3将图片上传到Bucket iOS应用

我试图从我的iOS应用上传图像到名为“img.haraj.com.sa”的存储桶。当我上传任何图片时,它们不会显示在存储区中。但是,当我将目标更改为名为“haraj”的存储桶时,它们会上传并显示在存储桶中。

这里的政策:

{ 
    "Statement": [ 
    { 
     "Sid": "**********hidden**********", 
     "Action": [ 
     "s3:GetObject", 
     "s3:PutObject" 
     ], 
     "Effect": "Allow", 
     "Resource": [ 
     "arn:aws:s3:::haraj/*" 
     ] 
    } 
    ] 
} 

我修改这个改变目标水桶。我还创建了名称为“img1.haraj.com.sa”的其他存储桶,并尝试上传图像,不幸的是它们也失败了。

看起来有一个问题,有点(。)和没有点桶的名称。没有圆点的水桶的名称可以与iOS应用程序一起使用,并且带点的名称不起作用。虽然我不确定。但我正面临着这个问题。我在应用程序代码中没有收到任何错误回复。

这里是我的iOS应用程序实现的一部分:

- (void)postAdButtonPushed:(id)sender 
{ 
    DLog(@"Post Ad") 

    AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:AWS_ACCESS_KEY_ID withSecretKey:AWS_SECRET_KEY]; 
    s3Client.timeout = 240; 

    NSString *bucketName = [NSString stringWithFormat:@"img.haraj.com.sa"]; 
    NSString *imageName = [NSString stringWithFormat:@"testimage.jpg"]; 

    S3PutObjectRequest *objReq = [[S3PutObjectRequest alloc] initWithKey:imageName inBucket:bucketName]; 
    objReq.contentType = @"image/jpeg"; 

    UIImage *testImageToUpload = [self.imagesToUpload objectAtIndex:0]; 

    NSData *imageData = UIImageJPEGRepresentation(testImageToUpload, 0.8); 
    objReq.data = imageData; 
    objReq.delegate = self; 

    objReq.contentLength = [imageData length]; 

    [s3Client putObject:objReq]; 
} 

- (void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response 
{ 
    DLog(@"response: %@", response.description) 
} 

- (void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error 
{ 
    DLog(@"Req failed: %@", error.description) 
} 

我也是在创建的线程在亚马逊论坛:AWS Upload image to Bucket iOS app

任何帮助,将不胜感激。谢谢!

+0

https://forums.aws.amazon.com/ message.jspa?messageID = 385925 – Eric 2013-02-25 15:56:41

+0

谢谢@Eric。该修补程序适用于Java版本。我在iOS中使用AWS SDK。我已经整合了框架(不是sdk的源代码)。让我们希望一些简单的解决方案出来,否则我将不得不解决与源代码的东西。 :\ – 2013-02-25 16:22:31

回答

6

我已经发布了对您的论坛主题here的回复,但总括而言,我相信您已经对SDK中的一个bug有所反应,并且需要明确设置存储桶所在的S3端点。

+0

感谢鲍勃!它的工作现在。我希望这在SDK的下一个版本中得到解决:) – 2013-02-26 10:21:24

+0

不幸的是,我不认为情况会如此。另外,在这种情况下,我们可能会采取冒泡的方式,所以不管您是否很可能需要明确地为这些情况设置端点。这实际上会帮助您的应用程序的性能,因为它不必处理重定向并发出第二个请求。 – 2013-02-26 16:35:34

+0

好的!也许在文档中提到这一点会更有帮助:) – 2013-03-03 13:37:34

6

只是想权衡,这里是我得到的工作

// #import <AWSS3/AWSS3.h> 
// #import <AWSRuntime/AWSRuntime.h> 
// then you should implement <AmazonServiceRequestDelegate> 
// import those in your .h file and 
// add the awss3 and awsruntime framework from the client 
// download from Amazon 
// myFace is the UIImage object 

    AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:@"Key_Goes_here" withSecretKey:@"Secret_Goes_Here"]; 

    NSString *imageName = [NSString stringWithFormat:@"%@.png", @"cpa"]; 

    S3PutObjectRequest *objReq = [[S3PutObjectRequest alloc] initWithKey:imageName inBucket:@"bucket_name"]; 
    objReq.contentType = @"image/png"; 
    objReq.cannedACL = [S3CannedACL publicRead]; 
    objReq.data = UIImagePNGRepresentation(myFace); 
    objReq.delegate = self; 

    [s3Client putObject:objReq]; 

这里的代码片断的委托方法:

-(void)request:(AmazonServiceRequest *)request didSendData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite { 
} 

-(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"response! %@", response); 
} 

-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response { 

} 

-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data { 
    NSLog(@"data?"); 
} 

-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error { 
    [self showAlertMessage:[error.userInfo objectForKey:@"message"] withTitle:@"Upload Error"]; 
} 
+4

haha​​hahahahh哦。有趣的堆栈溢出时刻。寻找如何做某事。找到您发布的代码。没有意识到你发布。尝试投票,因为它仍然是我能找到的唯一解决方案。 – chrisallick 2014-08-14 15:47:25