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

时间:2023-01-29 23:04:13

Having problems with the new Amazon SDK, https://github.com/aws/aws-sdk-ios-v2

遇到新的Amazon SDK问题,https://github.com/aws/aws-sdk-ios-v2

I'm just trying to write a file to an s3 bucket that already exists. Here's the code that I can't get to work for some reason (even though it logs "success").

我只是想把文件写入已经存在的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;
}];

The prior SDK (v1.x) this worked, but I'm trying to switch over because I like Bolts framework functionality.

之前的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];

Anyone out there playing with the new SDK who can tell me what I'm doing wrong here?

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

UPDATE - New Code Snippet

更新 - 新代码片段

 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;
}];

Thanks again.

再次感谢。

1 个解决方案

#1


3  

The body property of AWSS3PutObjectRequest needs to be either NSURL or NSData. If you convert the NSString to NSData like you are doing in v1 code snippet, it should work.

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

#1


3  

The body property of AWSS3PutObjectRequest needs to be either NSURL or NSData. If you convert the NSString to NSData like you are doing in v1 code snippet, it should work.

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