如何使用AWS的iOS SDK上传来自设备的图像,并设置为公开 [英] How to use AWS iOS SDK to upload image from device and set to public

查看:2099
本文介绍了如何使用AWS的iOS SDK上传来自设备的图像,并设置为公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我们似乎仅限于桶的数目,我想弄清楚如何完成以下任务:

Since it seems we are limited to the number of buckets, I'm trying to figure out how to accomplish the following:

  • 在我有一个iOS应用程序,用户可以上传个人资料图片。
  • 资料图片任何人都可以看到(我想它公开)。
  • 在理想情况下,我可以上传到一个桶(例如:myprofilepics.s3.amazonaws.com)
  • 在理想的情况下,每个用户可以上传自己的子文件夹(例如:myprofilepics.s3.amazonaws.com/images/userXXX /
  • 在理想情况下,我上传的图片,并将其设置为公共访问直接从应用程序,以便其他用户可以立即查看个人资料图片。

我缺少的东西在文档中?我AP preciate在这个问题上的任何反馈。

Am I missing something in the documentation? I appreciate any feedback on this issue.

推荐答案

要解决这个问题,我开始与亚马逊的样品code在他们的iOS SDK中发现的此处。在SDK的压缩,感兴趣的示例项目可以在样本中找到/ S3_Uploader

To solve this problem, I started with Amazon's sample code in their iOS SDK, found here. In the SDK zip, the sample project of interest can be found at samples/S3_Uploader.

要从示例项目得到在其中上传的图片是公共,你只需要添加在正确的地方一条线:

To get from that sample project to one in which the uploaded image is public, you simply need to add one line in the right place:

por.cannedACL   = [S3CannedACL publicRead];

其中, POR S3PutObjectRequest 用来上传的图像。

where por is the S3PutObjectRequest used to upload the image.

我的项目的code上传看起来像这样(看起来几乎相同,Amazon的样品code):

My project's code for uploading looks like this (looks almost identical to Amazon's sample code):

NSString *uuid = @""; // Generate a UUID however you like, or use something else to name your image.
UIImage *image; // This is the UIImage you'd like to upload.

// This URL is not used in the example, but it points to the file
// to be uploaded.
NSString *url = [NSString pathWithComponents:@[ @"https://s3.amazonaws.com/", AWS_PICTURE_BUCKET, uuid ]];

// Convert the image to JPEG data. Use UIImagePNGRepresentation for pngs
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

// Create the S3 Client.
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:AWS_ACCESS_KEY_ID withSecretKey:AWS_SECRET_KEY];

@try {
    // Create the picture bucket.
    [s3 createBucket:[[S3CreateBucketRequest alloc] initWithName:AWS_PICTURE_BUCKET]];

    // Upload image data.  Remember to set the content type.
    S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uuid inBucket:AWS_PICTURE_BUCKET];
    por.contentType = @"image/jpeg"; // use "image/png" here if you are uploading a png
    por.cannedACL   = [S3CannedACL publicRead];
    por.data        = imageData;
    por.delegate    = self; // Don't need this line if you don't care about hearing a response.

    // Put the image data into the specified s3 bucket and object.
    [s3 putObject:por];
}
@catch (AmazonClientException *exception) {
    NSLog(@"exception");
}

AWS_ACCESS_KEY_ID AWS_SECRET_KEY ,当然,您的AWS凭据, AWS_PICTURE_BUCKET 是你的照片桶。

AWS_ACCESS_KEY_ID and AWS_SECRET_KEY are, of course, your AWS credentials, and AWS_PICTURE_BUCKET is your picture bucket.

这篇关于如何使用AWS的iOS SDK上传来自设备的图像,并设置为公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆