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

Abd*_*mer 9 iphone objective-c amazon-s3 amazon-web-services ios

我是AWS新手,并将其用于iOS应用程序.

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

这是政策:

{
  "Statement": [
    {
      "Sid": "**********hidden**********",
      "Action": [
        "s3:GetObject",
        "s3:PutObject"
      ],
      "Effect": "Allow",
      "Resource": [
       "arn:aws:s3:::haraj/*"
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我修改它来更改目标存储桶.我还创建了名为"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)
}
Run Code Online (Sandbox Code Playgroud)

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

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

Bob*_*ney 6

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


chr*_*ick 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];
Run Code Online (Sandbox Code Playgroud)

这是委托方法:

-(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"];
}
Run Code Online (Sandbox Code Playgroud)

  • 哈哈哈哈哈哦.有趣的堆栈溢出时刻.寻找如何做某事.找到您发布的代码.没意识到你贴了.尝试投票,因为它仍然是我能找到的唯一解决方案. (5认同)