繁体   English   中英

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

[英]AWS S3 Upload image to Bucket iOS app

我是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)
}

我还在Amazon Forum上创建了一个主题: AWS将图像上传到Bucket iOS应用程序

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

我已经张贴到您的论坛帖子的响应这里 ,而是要总结,我相信你已经碰到了针对SDK中的一个bug,需要明确设置S3终点在那里你的水桶所在。

只是想要权衡,这是我工作的代码片段

// #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"];
}

如果您不想授予公共访问权限,则还应在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/*"
    }
  ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM