2017-10-05 1 views
1

AWS를 제 3 자로 사용하고 있습니다. 고객 키로 서버 측 암호화를 설정하려고합니다. 다음 코드를 사용하고 있지만 항상 "키의 계산 된 MD5 해시가 제공된 해시와 일치하지 않습니다."라는 메시지가 나타납니다.고객 키로 AWS 이미지 업로드

여기

{ 

ArgumentName = "x-amz-server-side-encryption"; 

    ArgumentValue = null; 
    Code = InvalidArgument; 
    HostId = "sXhuMvE1HRXShELxHiYh6bSoTBn/JYc1DVVD/TfZ2UpAuNQ4IYnR9ptr0ENPCgUO8iGmNw23lBM="; 
    Message = "The calculated MD5 hash of the key did not match the hash that was provided."; 
    RequestId = A1303BF60BAF1F06; 
} 

"ABCDEF"= 고객 키를 사용하고 내 아이폰 OS 코드

-(void)uploadAsset:(UIImage*)image filePath:(NSString*)filePath key:(NSString*)customerKey success:(successCallback)success andfailure:(failureCallback)failure { 

    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image"]; 
    NSData *imageData = UIImagePNGRepresentation(image); 
    [imageData writeToFile:path atomically:YES]; 
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; 
    // next we set up the S3 upload request manager 
    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new]; 
    // set the bucket 
    uploadRequest.bucket = [AppConfigManager getInstance].getApp.bucketName; 
    uploadRequest.SSECustomerAlgorithm = @"AES256"; 
    // I want this image to be public to anyone to view it so I'm setting it to Public Read 
    // uploadRequest.ACL = AWSS3ObjectCannedACLAuthenticatedRead; 
    // set the image's name that will be used on the s3 server. I am also creating a folder to place the image in 
    uploadRequest.key = filePath; 

    uploadRequest.SSECustomerKey = @"abcdef"; 
    // NSString *base64EncodedString = [[[[AWSHelper instance] md5:customerKey] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0]; 

    uploadRequest.SSECustomerKeyMD5 = [[AWSHelper instance] md5:customerKey]; 
    uploadRequest.serverSideEncryption = AWSS3ServerSideEncryptionUnknown; 
    // set the content type 
    uploadRequest.contentType = @"image/png"; 
    // we will track progress through an AWSNetworkingUploadProgressBlock 
    uploadRequest.body = url; 

    uploadRequest.uploadProgress =^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){ 
     dispatch_sync(dispatch_get_main_queue(), ^{ 

     }); 
    }; 

    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager]; 

    [[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) { 
     if (task.error) { 
      if (failure) { 
       failure(task.error); 
       NSLog(@"error %@",task.error.userInfo); 
      } 
     } 
     else{ 
      if (success) { 
       success(task); 
      } 
     } 

     return nil; 
}]; 

} 

답변

0

우리는 키 키 길이가 있음을주의해야 키를 사용하여 파일을 업로드를 들어 32

-(void)uploadAsset:(UIImage*)image filePath:(NSString*)filePath key:(NSString*)customerKey success:(successCallback)success andfailure:(failureCallback)failure { 

     NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image"]; 
     NSData *imageData = UIImagePNGRepresentation(image); 
     [imageData writeToFile:path atomically:YES]; 
     NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; 

     AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new]; 
     uploadRequest.bucket = [AppConfigManager getInstance].getApp.bucketName; 
     uploadRequest.SSECustomerAlgorithm = @"AES256"; 
     uploadRequest.ACL = AWSS3ObjectCannedACLAuthenticatedRead; 
     uploadRequest.key = filePath; 

     //Custom SSE 
     NSString *key = customerKey; 
     NSString *base64Key =[[key dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];// 
     NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:base64Key options:kNilOptions]; 
     NSString *base64KeyMD5 = [NSString aws_base64md5FromData:nsdataFromBase64String]; 
     NSString *base64EncodedString = [[key dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0]; 
     NSLog(@"base64EncodedString == %@",base64EncodedString); 


     uploadRequest.SSECustomerKey = base64Key; 
     uploadRequest.SSECustomerKeyMD5 = base64KeyMD5 ; 

     // set the content type 
     uploadRequest.contentType = @"image/png"; 
     // we will track progress through an AWSNetworkingUploadProgressBlock 
     uploadRequest.body = url; 
     __block int64_t totalDownloadedBytes = 0; 
     __block int64_t totalExpectedDownloadBytes = 0; 

     uploadRequest.uploadProgress =^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){ 
      dispatch_sync(dispatch_get_main_queue(), ^{ 
       totalDownloadedBytes = totalBytesSent; 
       totalExpectedDownloadBytes = totalBytesExpectedToSend; 
       NSLog(@"bytesWritten: totalBytesWritten: %lld, totalBytesExpectedtoWrite: %lld",totalBytesSent,totalBytesExpectedToSend); 
      }); 
     }; 

     AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager]; 

     [[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) { 
      if (task.error) { 
       if (failure) { 
        failure(task.error); 
        NSLog(@"error %@",task.error.userInfo); 
       } 
      } 
      else{ 
       if (success) { 
        success(task); 
       } 
      } 

      return nil; 
     }]; 
    }