简体   繁体   中英

AWS image uploading with customerKey

I am using AWS as third party. I want to set server side encryption with customer-key. I am using following code but always getting message "The calculated MD5 hash of the key did not match the hash that was provided."

I am using customer key ="abcdef"

{

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;
}

here is my iOS code

-(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;
}];

}

For uploading file with key we must take care that key key length is 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;
        }];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM