Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get AWSS3PutObjectRequest in v2 AWS SDK to duplicate S3PutObjectRequest in v1 SDK

Having problems with the new 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").

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.

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?

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.

like image 803
jeremyclover Avatar asked Jun 20 '14 04:06

jeremyclover


People also ask

How do I get content from S3Object?

You can get the object's contents by calling getObjectContent on the S3Object . This returns an S3ObjectInputStream that behaves as a standard Java InputStream object.

How do I move files from one S3 bucket to another in Java?

There is no direct implementation of a rename or move operation in S3. Instead, the typical solution is to copy the object to the new location and then delete the original. You can accomplish this with the AmazonS3#copyObject and AmazonS3#deleteObject methods of the AWS SDK for Java.

What is S3ObjectSummary?

public class S3ObjectSummary extends Object implements Serializable. Contains the summary of an object stored in an Amazon S3 bucket. This object doesn't contain the object's full metadata or any of its contents.


1 Answers

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.

like image 93
Yosuke Matsuda Avatar answered Sep 19 '22 12:09

Yosuke Matsuda