在 iOS 上上传文件到 S3 的简单方法。我之所以写这个,是因为那时我无法让 AFAmazonS3Client 工作。
我们现在使用 Cocoapods(您也应该使用)。将其添加到您的 Podfile
中,然后您就可以开始使用了。
pod 'OWS3Client'
这个库依赖于 Amazon 的 iOS SDK,并由 podspec 自动链接。
NSString *filePath = @"/path/to/file.txt";
NSString *bucketName = @"bucket-name";
NSString *key = @"file.txt";
OWS3Client *s3Client = [[OWS3Client alloc] initWithAccessKey:AWS_ACCESS_KEY_ID secretKey:AWS_SECRET_KEY];
[s3Client postObjectWithFile:filePath bucket:bucketName key:key acl:@"public-read" success:^(S3PutObjectResponse *responseObject) {
NSLog(@"success updating manifest after uploading %@", outputFileName);
} failure:^(NSError *error) {
NSLog(@"error uplaoding manifest after %@", outputFileName);
}];
The MIT License (MIT)
Copyright (c) 2013 OpenWatch, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.