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

时间:2021-05-12 10:47:29

I am new to AWS and using it for iOS app.

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

I'm trying to upload images from my iOS app to bucket named, "img.haraj.com.sa". When I upload any image, they aren't shown in bucket. But when I change the target to bucket named "haraj", they are uploaded and shown in the bucket.

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

Here's the policy:

这是政策:

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

I modify this for changing the target bucket. I also created other buckets with name "img1.haraj.com.sa" and tried uploading images and unfortunately they also failed.

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

It seems there is some problem with having bucket names with dots (.) and without dots. Names of buckets without dots works with iOS app and the names with dots doesn't work. I'm not sure though. But I'm facing this problem. I don't receive any error response in app code.

使用点(。)和没有点的桶名称似乎存在一些问题。没有圆点的存储桶名称适用于iOS应用程序,带点的名称不起作用。我不确定。但是我正面临着这个问题。我在应用程序代码中没有收到任何错误响应。

Here's part of my iOS app implementation:

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

I also created a thread on Amazon Forum at: AWS Upload image to Bucket iOS app

我还在Amazon Forum上创建了一个线程:AWS将图像上传到Bucket iOS app

Any help would be appreciated. Thank you!

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

2 个解决方案

#1


6  

I've posted a response to your forum thread here, but to summarize, I believe you've bumped up against a bug in the SDK and will need to explicitly set the S3 endpoint where your bucket is located.

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

#2


6  

Just wanted to weigh in, here is a code snippet that i got working

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

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

here are the delegate methods:

这是委托方法:

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

#1


6  

I've posted a response to your forum thread here, but to summarize, I believe you've bumped up against a bug in the SDK and will need to explicitly set the S3 endpoint where your bucket is located.

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

#2


6  

Just wanted to weigh in, here is a code snippet that i got working

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

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

here are the delegate methods:

这是委托方法:

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