AWS S3将图像上传到Bucket iOS应用程序

时间:2013-02-25 14:53:27

标签: iphone ios objective-c amazon-web-services amazon-s3

我是AWS新手并将其用于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

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

3 个答案:

答案 0 :(得分:6)

我已经发布了对您的论坛帖子here的回复,但总而言之,我相信您已经遇到了SDK中的错误,需要明确设置您的存储桶所在的S3端点

答案 1 :(得分: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"];
}

答案 2 :(得分:0)

如果您不想授予公共访问权限,则还应该在IAM中定义一个用户,并将此代码放入存储桶策略中:

{
  "Version": "2012-10-17",
  "Id": "S3AccessPolicy",
  "Statement": [
    {
      "Sid": "GiveGetPutAccess",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:user/YOUR_USER"
      },
      "Action": [
        "s3:GetObject",
        "s3:PutObject"
      ],
      "Resource": "arn:aws:s3:::YOUR_BUCKET/*"
    }
  ]
}