1

尝试将大于 5 mb 的文件上传到 Amazon S3

http://aws.amazon.com/articles/0006282245644577,这里说

// The S3UploadInputStream was deprecated after the release of iOS6

5 mb 以下的文件我可以通过 wifi 轻松上传:

NSData *dataForAamzon = [[NSFileManager defaultManager] contentsAtPath:pathForFiile];
@try {
        NSString *uploadName= @"somestring";


        S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uploadName
                                                                 inBucket:[Constants userEventBucket]];
        por.contentType = nil;
        por.data        = dataForAamzon;



        // Put the image data into the specified s3 bucket and object.
        S3PutObjectResponse *putObjectResponse = [self.s3 putObject:por];

        [self performSelector:@selector(uploadAllFilesToAmazon:error:) withObject:nil withObject:putObjectResponse.error];

    }
    @catch ( AmazonServiceException *exception ) {
        NSLog( @"Upload Failed, Reason: %@", exception );
    }

由于S3UploadInputStream *stream已弃用,如何检查文件是否大于 5mb 并在 IOS 6 或更高版本中使用分段上传。

谢谢,

4

2 回答 2

0

我希望您需要检查本地文件的大小超过 5MB 对吗?

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:_client.filename error:nil];
if(fileAttributes)
{
    NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
    long totalFileSize = [fileSizeNumber longLongValue];
    if(totalFileSize > 5*1024*1024)
    // Go for multipart upload
    else 
    // Go for Direct Put request
}

或者从 NSData 您可以获取 Length 并执行相同的计算。

于 2013-04-23T14:40:25.903 回答
-1

AWS SDK 的新版本解决了我的问题。

新的 AWS SDK 正在自动处理文件大小,您需要创建传输管理器变量并将其委托给 s3 对象

就像是

-(void) uploadAgendatoAmazon
{
    //pass string in memory to nsdictionary
    NSData * data = [_uploadPlistString dataUsingEncoding:NSUTF8StringEncoding];

    NSString *uploadName= [NSString stringWithFormat:@"%@/%@/agenda.plist",[[NSUserDefaults standardUserDefaults] valueForKey:@"email"],self.textEventName.text];

    self.s3.maxRetries = 10;
    self.s3.timeout = 60;
    self.tm = [S3TransferManager new];
    self.tm.s3 = self.s3;
    self.tm.operationQueue.maxConcurrentOperationCount = 2;

   S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uploadName
                                                                 inBucket:[Constants userEventBucket]];
    por.contentType = nil;
    por.data        = data;
    por.delegate = self;
    por.requestTag = @"uploadAgendatoAmazon";
    // Put the image data into the specified s3 bucket and object.
    [self.tm upload:por];
}

这是有关完整详细信息的博客文章; http://mobile.awsblog.com/post/TxIRFEQTW9XU8G/S3TransferManager-for-iOS-Part-I-Asynchronous-Uploads

于 2013-06-20T17:45:57.913 回答