【问题标题】:How to use AWS iOS SDK to upload image from device and set to public如何使用 AWS iOS SDK 从设备上传图像并设置为公共
【发布时间】:2012-06-17 21:09:17
【问题描述】:

由于我们似乎受限于存储桶的数量,我试图弄清楚如何完成以下工作:

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

我是否遗漏了文档中的某些内容?感谢您对此问题的任何反馈。

【问题讨论】:

    标签: ios image file-upload permissions amazon-s3


    【解决方案1】:

    为了解决这个问题,我从亚马逊的 iOS SDK 中的示例代码开始,找到了here。在 SDK zip 中,可以在 samples/S3_Uploader 找到感兴趣的示例项目。

    要从该示例项目转到上传图片公开的项目,您只需在正确的位置添加一行:

    por.cannedACL   = [S3CannedACL publicRead];
    

    其中por 是用于上传图片的S3PutObjectRequest

    我的项目上传代码是这样的(看起来几乎和亚马逊的示例代码一模一样):

    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_IDAWS_SECRET_KEY 当然是您的 AWS 凭证,AWS_PICTURE_BUCKET 是您的图片存储桶。

    【讨论】:

    • 非常感谢。我要试试这个。我最终选择了一个我并不喜欢的解决方案,这听起来容易多了!
    • 嗨,谢谢你的代码。我在没有内容类型的情况下尝试了它并工作了。为什么有必要?
    • 快速搜索表明,虽然在不指定 MIME 内容类型的情况下上传可能会正常工作,但如果未正确指定,您以后可能会或可能不会遇到问题。 (亚马逊可能也猜到了,我不知道。在上传时指定它似乎无害。)
    • 你在哪里使用你在这里包含的 NSString *url?
    • @isaac 不错!在这个例子中我实际上并没有使用这个变量,但我相信它是指向刚刚上传的文件的 URL。我已经在我的回答中澄清了。
    【解决方案2】:

    如果您要管理自己用户的身份 - 也就是说,不是通过 AIM - 您还必须管理有权访问的资源。我会加入一个瘦 Web 服务来管理对 S3 上文件的访问。

    或者,您可以使用Temporary Security Credentials 和安全策略为您的用户提供对 S3 的访问权限。您必须在设备上添加代码才能为您的用户获取临时令牌。

    我会选择第一个解决方案,因为它使 Amazon S3 不在等式中,并且您可以在稍后阶段腾出双手来选择另一个后端。

    【讨论】:

    • 我希望有更多 CDN 类型的解决方案,我认为 S3 提供了这种解决方案......从某种意义上说,我上传了一张图片,将其公开,然后存储一个可公开查看的图片 url只要您知道 URL 就可以查看...
    • 或者,我可以以某种方式设置一个可公开访问的“存储桶”,以便可以查看添加到该存储桶的任何对象吗?在这种情况下,我只需创建一个名为“myprofileimages”的存储桶,并以某种方式设置权限策略以允许任何人仅查看项目?
    • stackoverflow.com/questions/24349088/… 请你检查一下这个问题,因为我面临一些问题
    【解决方案3】:

    这就是我最终做的:

        BOOL success = YES;
    
    // Initialize the S3 Client.
    AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
    
    @try {
        // Create the picture bucket.
        [s3 createBucket:[[S3CreateBucketRequest alloc] initWithName:HOSTED_BUCKET]];
    
        NSString *remoteImagePath = [self pathWithImageType:SFHostedImageTypeProfile identiefier:@""];
    
    
        // Upload image data.  Remember to set the content type.
        S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:remoteImagePath inBucket:HOSTED_BUCKET];
        por.contentType = @"image/png";
    
        por.data        = UIImagePNGRepresentation(image);
    
        // Put the image data into the specified s3 bucket and object.
        [s3 putObject:por];
    }
    @catch (AmazonClientException *exception) {
        //        [Constants showAlertMessage:exception.message withTitle:@"Upload Error"];
        NSLog(@"Save Hosted Image Exception: %@", exception.message);
    
        success = NO;
    }
    
    return success;
    

    【讨论】:

    猜你喜欢
    • 2018-09-29
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    相关资源
    最近更新 更多