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:AWSRegionUSEast1service:AWSServiceS3URL:[NSURL URLWithString:endPoint]];NSLog(@"czf007: requestURL: %@", endpoint.URL.absoluteString);AWSStaticCredentialsProvider *credentialsProvider =[[AWSStaticCredentialsProvider alloc] initWithAccessKey:accessKeysecretKey:secretKey];AWSServiceConfiguration *configuration =[[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1endpoint:endpointcredentialsProvider: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:putRequestcompletionHandler:^(AWSS3PutObjectOutput * _Nullable response, NSError * _Nullable error) {if (error) {NSLog(@"czf007:upload failed: %@", error);} else {NSLog(@"czf007:upload success!");}}];}
3. 查看日志
4. 在Minio后台查看文件是否上传成功