IT评测·应用市场-qidao123.com技术社区
标题:
iOS中使用AWS上传zip文件到Minio上的oss平台上
[打印本页]
作者:
海哥
时间:
4 天前
标题:
iOS中使用AWS上传zip文件到Minio上的oss平台上
1. 集成AWS相关库(万万不要用最新的版本,否则会出现风格化虚拟路径,找不到主机名)
pod 'AWSS3', '~> 2.10.0'
pod 'AWSCore', '~> 2.10.0'
复制代码
2. 编写集成的相关代码
- (void)uploadFileToMinIO {
NSString *endPoint = @"http://192.168.1.31:9000"; // 有的需要加端口,有的不需要
NSString *accessKey = @"FDX7fP3XbXXCvwbAQfefUzOiGf"; // minio后台申请的
NSString *secretKey = @"QrtJww9vcEy383126ZRX4LxxSNo2iC03yy5lbnJQnTcA"; // minio后台申请的
NSString *bucketName = @"桶名"; // 从minio后台创建
AWSEndpoint *endpoint = [[AWSEndpoint alloc] initWithRegion:AWSRegionUSEast1
service:AWSServiceS3
URL:[NSURL URLWithString:endPoint]];
NSLog(@"czf007: requestURL: %@", endpoint.URL.absoluteString);
AWSStaticCredentialsProvider *credentialsProvider =
[[AWSStaticCredentialsProvider alloc] initWithAccessKey:accessKey
secretKey:secretKey];
AWSServiceConfiguration *configuration =
[[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
endpoint:endpoint
credentialsProvider:credentialsProvider];
// [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration; // 这个不能设置,会导致走虚拟化路径,使用下面的方式即可
[AWSS3 registerS3WithConfiguration:configuration forKey:@"MinIO"];
AWSS3PutObjectRequest *putRequest = [AWSS3PutObjectRequest new];
putRequest.bucket = bucketName;
putRequest.key = @"yourfile.zip";
putRequest.body = [NSURL fileURLWithPath:@"/work/redsocks.zip"];
putRequest.contentType = @"application/zip";
// 设置 contentLength,防止 ExcessData 错误
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:@"/work/redsocks.zip" error:nil];
NSNumber *fileSizeNumber = [attrs objectForKey:NSFileSize];
putRequest.contentLength = fileSizeNumber;
NSLog(@"czf007: uploading file with size: %@", fileSizeNumber);
[[AWSS3 S3ForKey:@"MinIO"] putObject:putRequest
completionHandler:^(AWSS3PutObjectOutput * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"czf007:upload failed: %@", error);
} else {
NSLog(@"czf007:upload success!");
}
}];
}
复制代码
3. 查看日志
4. 在Minio后台查看文件是否上传成功
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/)
Powered by Discuz! X3.4