IT评测·应用市场-qidao123.com技术社区

标题: iOS中使用AWS上传zip文件到Minio上的oss平台上 [打印本页]

作者: 海哥    时间: 4 天前
标题: iOS中使用AWS上传zip文件到Minio上的oss平台上
1. 集成AWS相关库(万万不要用最新的版本,否则会出现风格化虚拟路径,找不到主机名)
  1. pod 'AWSS3', '~> 2.10.0'
  2. pod 'AWSCore', '~> 2.10.0'
复制代码
2. 编写集成的相关代码
  1. - (void)uploadFileToMinIO {
  2.     NSString *endPoint = @"http://192.168.1.31:9000";        // 有的需要加端口,有的不需要
  3.     NSString *accessKey = @"FDX7fP3XbXXCvwbAQfefUzOiGf";    //  minio后台申请的
  4.     NSString *secretKey = @"QrtJww9vcEy383126ZRX4LxxSNo2iC03yy5lbnJQnTcA"; //  minio后台申请的
  5.     NSString *bucketName = @"桶名"; // 从minio后台创建
  6.    
  7.    
  8.     AWSEndpoint *endpoint = [[AWSEndpoint alloc] initWithRegion:AWSRegionUSEast1
  9.                                                         service:AWSServiceS3
  10.                                                            URL:[NSURL URLWithString:endPoint]];
  11.     NSLog(@"czf007: requestURL: %@", endpoint.URL.absoluteString);
  12.     AWSStaticCredentialsProvider *credentialsProvider =
  13.         [[AWSStaticCredentialsProvider alloc] initWithAccessKey:accessKey
  14.                                                       secretKey:secretKey];
  15.     AWSServiceConfiguration *configuration =
  16.         [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
  17.                                                endpoint:endpoint
  18.                                     credentialsProvider:credentialsProvider];
  19.    
  20. //    [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;    // 这个不能设置,会导致走虚拟化路径,使用下面的方式即可
  21.     [AWSS3 registerS3WithConfiguration:configuration forKey:@"MinIO"];
  22.     AWSS3PutObjectRequest *putRequest = [AWSS3PutObjectRequest new];
  23.     putRequest.bucket = bucketName;
  24.     putRequest.key = @"yourfile.zip";
  25.     putRequest.body = [NSURL fileURLWithPath:@"/work/redsocks.zip"];
  26.     putRequest.contentType = @"application/zip";
  27.    
  28.     // 设置 contentLength,防止 ExcessData 错误
  29.     NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:@"/work/redsocks.zip" error:nil];
  30.     NSNumber *fileSizeNumber = [attrs objectForKey:NSFileSize];
  31.     putRequest.contentLength = fileSizeNumber;
  32.     NSLog(@"czf007: uploading file with size: %@", fileSizeNumber);
  33.     [[AWSS3 S3ForKey:@"MinIO"] putObject:putRequest
  34.         completionHandler:^(AWSS3PutObjectOutput * _Nullable response, NSError * _Nullable error) {
  35.             if (error) {
  36.                 NSLog(@"czf007:upload failed: %@", error);
  37.             } else {
  38.                 NSLog(@"czf007:upload success!");
  39.             }
  40.     }];
  41. }
复制代码
3. 查看日志

4. 在Minio后台查看文件是否上传成功


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4