美文网首页
iOS上传文件到minio

iOS上传文件到minio

作者: 东也_ | 来源:发表于2023-09-04 16:24 被阅读0次

    配置信息:

    • SDK AWSS3: 2.33.4
    • Language: OC

    全部代码如下:

    本机搭建的minio服务

    AWSDDLog.sharedInstance.logLevel = AWSDDLogLevelVerbose;
        [AWSDDLog addLogger:AWSDDTTYLogger.sharedInstance];
        AWSStaticCredentialsProvider *provider = [[AWSStaticCredentialsProvider alloc]
                                                  initWithAccessKey:@"WLZO9XjvNdBjsbKppE36" secretKey:@"FpFIhYUoxpGGTUiSrHMUl78ugSuMQ5k2ZscFht4W"];
        AWSEndpoint *minioEndpoint = [[AWSEndpoint alloc] initWithRegion:AWSRegionUSWest2 service:AWSServiceS3 URL:
                                      [NSURL URLWithString:@"http://192.168.65.202:9000"]];
        AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2
                                                                                        endpoint:minioEndpoint credentialsProvider:provider localTestingEnabled:true];
        configuration.maxRetryCount = 1;
        configuration.timeoutIntervalForRequest = 10;
        configuration.timeoutIntervalForResource = 10;
        AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
        
        [AWSS3 registerS3WithConfiguration:configuration forKey:@"USWest2S3"];
        
        NSMutableString *dataString = [NSMutableString new];
        for (int32_t i = 1; i < 10000000; i++) {
            [dataString appendFormat:@"%d\n", i];
        }
        NSData *testData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
        
        AWSS3 *S3 = [AWSS3 S3ForKey:@"USWest2S3"];
        
        NSString *fileName= @"ios_test.txt";
       
        AWSS3PutObjectRequest *request = [[AWSS3PutObjectRequest alloc] init];
        request.bucket = @"ios.bucket.test";
        request.key = fileName;
        request.contentType = @"text/plain";
        request.ACL = AWSS3ObjectCannedACLPublicRead;
        request.body = testData;
        request.contentLength = @(testData.length);
        [S3 putObject:request completionHandler:^(AWSS3PutObjectOutput * _Nullable response, NSError * _Nullable error) {
            
            if (!error) {
                NSLog(@"succeed");
            }else{
                NSLog(@"failed: %@", error.localizedDescription);
            }
        }];
    

    注意:

    • 我不确定是否是SDK的bug,还是我的request设置有问题,因为网上以及官方都没有相关参考代码,在源码中我也没找到原因;必须更改SDK中AWSSignature文件中的第239行代码,其中一个判断设计到授权信息的设置,默认是会连接失败,因为我参考了flutter的minio,其中x-amz-content-sha256的值是不对的,所以必须得更改这个判断;
      图片总是上传失败,只能贴这个代码了
    if (nil == stream) {
    
    • 服务端bucket的名称设置格式要注意,不然会导致url拼接错误,最好用我那种方式;

    相关文章

      网友评论

          本文标题:iOS上传文件到minio

          本文链接:https://www.haomeiwen.com/subject/elkcvdtx.html