ios - 如何使用 AWS iOS SDK v2 将 UIImage 上传到 S3

标签 ios objective-c amazon-web-services amazon-s3 uiimage

Github 中的 README 页面 ( https://github.com/aws/aws-sdk-ios-v2 ) 已经有一个从文件路径 URL 上传图片的示例:

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = yourBucket;
uploadRequest.key = yourKey;
uploadRequest.body = yourDataURL; // <<<< this is a NSURL
uploadRequest.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];

但是,如果我在内存中只有一个 UIImage(没有文件路径)怎么办? 是否可以使用 SDKUIImage(或其 NSData)上传到 S3

手动使用 HTTP API(使用 AFNetworking 之类的东西)会更容易吗?

最佳答案

虽然AWSiOSSDKv2不支持从内存上传图片,但您可以将其保存为文件,然后再上传。

//image you want to upload
UIImage* imageToUpload = [UIImage imageNamed:@"imagetoupload"]; 

//convert uiimage to 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", dateKey]];
[UIImagePNGRepresentation(imageToUpload) writeToFile:filePath atomically:YES];

NSURL* fileUrl = [NSURL fileURLWithPath:filePath];

//upload the image
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.body = fileUrl;
uploadRequest.bucket = AWS_BUCKET_NAME;
uploadRequest.key = @"yourkey";
uploadRequest.contentType = @"image/png";
[[transferManager upload:thumbNailUploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
    if(task.error == nil) {
        NSLog(@"woot");
    }
    return nil;
}];

关于ios - 如何使用 AWS iOS SDK v2 将 UIImage 上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369602/

相关文章:

iOS swift : ScrollView strict dragging

objective-c - UILocalizedIndexedCollat​​ion 不显示中文字符

javascript - S3 :HeadObject - returns 403 response

amazon-s3 - 如何将镜像传输到 Amazon EBS 卷以供 EC2 使用?

ios - 调整弹出窗口 ViewController 的大小以适应要在 UITableView 中显示的数据

ios - CFBundleDisplayName 返回 'null'

ios - Segue 标识符崩溃问题

ios - 如何改变UIWebview的字体颜色、字体名称和字体大小?

ios - uitableview如何动态解析数据多单元格

python - 无法使用 boto 2.31.1 创建 cloudsearch int 索引字段