适用场景
如果要使用自建的对象服务, 该服务兼容AWS的协议, 我们该如何在
遇到的困难
- AWS通过CocoaPod继承下载过程非常费时
- 自建的服务其实不支持远程校验服务, 移动端需要承担的一定风险
主要实现
- 下载AWS的iOS端framework https://amazonaws-china.com/cn/mobile/resources/
- 手动集成到项目, 可以参考这里
- 在代码中创建并注册AWSS3TransferUtility
AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:@"YourAccessKey"
secretKey:@"YourSecretKey"];
AWSEndpoint *endPoint = [[AWSEndpoint alloc] initWithURLString:@"http://<your domain>"];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2
endpoint:endPoint
credentialsProvider:credentialsProvider];
[AWSS3TransferUtility registerS3TransferUtilityWithConfiguration:configuration forKey:@"GoodName"]; //自己定义
- 上传文件(或别的操作)
NSString *content =@"milo test test";
NSData *testData = [content dataUsingEncoding:NSUTF8StringEncoding];
//获取AWSS3TransferUtility, 注意要跟上文注册时的名字一致
AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility S3TransferUtilityForKey:@"GoodName"];
[[transferUtility uploadData:testData
bucket:@"YourBucketName"
key:@"keyname" //自己定义的
contentType:@"text/plain"
expression:nil
completionHandler:self.completionHandler] continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"Error: %@", task.error);
}
if (task.result) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Uploading...");
});
}
return nil;
}];
网友评论