繁体   English   中英

如何将图像上传到亚马逊 s3 服务器,我在 iOS 中使用 Objective c 拥有密钥、访问密钥和存储桶名称?

[英]How to upload images to amazon s3 server , I have the secret key, Accesskey and bucket name in iOS using objective c?

在这里,我有密钥、访问密钥和存储桶名称,这些凭据足以存储和从 amazon s3 服务器获取图像还是需要其他任何凭据? 我通过以下链接安装了 cocoapods:

http://docs.aws.amazon.com/mobile/sdkforios/developerguide/setup-aws-sdk-for-ios.html

我面临的问题是无法推进我的工作,请提供任何逐步成功的过程?

您可以参考适用于 iOS 的 Amazon S3 Transfer Manager 的文档,其中包含有关 SDK 与如何从 S3 存储上传和下载图像的集成的所有信息。

@property (nonatomic,strong) AWSS3TransferManager *transferManager;

-(void)uploadImage:(UIImage*)img
{
    NSString *bucketName = @"bucket_Name";
    NSString *fileName = [[[NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000] stringByReplacingOccurrencesOfString:@"." withString:@""] stringByAppendingString:@".png"];
    fileName = [@"ios" stringByAppendingString:fileName];
    NSString *path = [NSTemporaryDirectory() stringByAppendingString:fileName];
    NSData *imageData = UIImagePNGRepresentation(img);
    NSUInteger size = imageData.length;
    [imageData writeToFile:path atomically:YES];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];

    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
    uploadRequest.bucket = bucketName;
    uploadRequest.key = fileName;
    uploadRequest.contentType = @"image/png";
    uploadRequest.body = url;
    uploadRequest.contentLength = [NSNumber numberWithUnsignedLongLong:size];

    uploadRequest.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (totalBytesExpectedToSend > 0) {

                weakself.file_size = totalBytesExpectedToSend;
                weakself.file_uploaded = totalBytesSent;

                [weakself updateProgress];
            }
        });
    };

    [[_transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock: ^id(AWSTask *task){
        if (task.error)
        {
            NSLog(@"ERROR : %@",task.error);
        }
        else{
            NSLog(@"Uploaded on : \n https://s3_path_for.amazonaws.com/%@/%@", bucketName, fileName);
            UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Uploaded :)"
                                                                  message:@""
                                                                 delegate:nil
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles: nil];
        }
        return nil;
    }];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM