ios - 无法在 v2 AWS SDK 中获取 AWSS3PutObjectRequest 以复制 v1 SDK 中的 S3PutObjectRequest

标签 ios amazon-web-services amazon-s3

新的亚马逊 SDK 有问题,https://github.com/aws/aws-sdk-ios-v2

我只是想将文件写入已存在的 s3 存储桶。这是由于某种原因我无法开始工作的代码(即使它记录了“成功”)。

AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:@"KEY" secretKey:@"SECRET_KEY"];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

AWSS3 *s3 = [[AWSS3 alloc] initWithConfiguration:configuration];

AWSS3PutObjectRequest *logFile = [AWSS3PutObjectRequest alloc];
logFile.bucket = @"test";
logFile.key = @"file2";
logFile.contentType = @"text/plain";
logFile.body = @"this is a test";
[[s3 putObject:logFile] continueWithBlock:^id(BFTask *task) {
    NSLog(@"Totally did it");
    return nil;
}];

之前的 SDK (v1.x) 工作正常,但我正在尝试切换,因为我喜欢 Bolts 框架功能。

AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:@"KEY" withSecretKey:@"SECRET_KEY"];
S3PutObjectRequest *logFile = [[S3PutObjectRequest alloc] initWithKey:@"file1" inBucket:@"test"];
logFile.contentType = @"text/plain";
    NSString* myStuff = @"this is a test";
NSData* log = [myStuff dataUsingEncoding:NSUTF8StringEncoding];
logFile.data = log;
[s3 putObject:logFile];

有人在玩新的 SDK 可以告诉我这里做错了什么吗?

更新 - 新代码片段

 AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:awsAccessKey secretKey:awsSecretKey];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

/* This section works but posts as text/xml
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
AWSS3TransferManagerUploadRequest *getLog = [AWSS3TransferManagerUploadRequest new];
*/
AWSS3 *transferManager = [[AWSS3 alloc] initWithConfiguration:configuration];
AWSS3PutObjectRequest *getLog = [AWSS3PutObjectRequest alloc];
getLog.bucket = awsS3Bucket;
getLog.key = awsS3FileNameString;
getLog.contentType = @"text/plain";
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:logFileName];
long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileName error:nil][NSFileSize] longLongValue];
getLog.body = [NSURL fileURLWithPath:fileName];
getLog.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];

/* 
[[transferManager upload:getLog] continueWithBlock:^id(BFTask *task) {
*/

[[transferManager putObject:getLog] continueWithBlock:^id(BFTask *task) {        
    if(task.error)
    {
        NSLog(@"Error: %@",task.error);
    }
    else
    {
        NSLog(@"Got here: %@", task.result);

    }
    return nil;
}];

再次感谢。

最佳答案

AWSS3PutObjectRequestbody 属性需要是NSURLNSData。如果像在 v1 代码片段中那样将 NSString 转换为 NSData,它应该可以工作。

关于ios - 无法在 v2 AWS SDK 中获取 AWSS3PutObjectRequest 以复制 v1 SDK 中的 S3PutObjectRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319867/

相关文章:

ios - 在 iOS AVPlayer 中,addPeriodicTimeObserverForInterval 似乎丢失了

ios - Xcode (Swift) - 如何在按钮上存储图像文件的名称

python-3.x - AWS Textract 无法识别 PDF 文档第二页的表格

amazon-s3 - 以编程方式创建 s3cmd 配置文件

hadoop - 使用 hadoop 从 S3 'requester pay' 存储桶下载数据时出错

ios - UILabel 删除线与 UIImage - NSAttributedString

ios - TTTAttributedLabel,颜色更改仅在第一次在 Swift 中启动 Controller 时起作用

node.js - nginx Node 上游在读取响应 header 时过早关闭连接

javascript - 无法在 Ajax 调用中将图像上传到 Amazon S3

node.js - AWS S3 使用 URL 获取对象