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后臺查看文件是否上傳成功