使用customerKey

时间:2017-10-05 07:19:47

标签: ios objective-c amazon-web-services

我使用AWS作为第三方。我想用客户密钥设置服务器端加密。我正在使用以下代码,但始终收到消息“密钥的计算MD5哈希值与提供的哈希值不匹配。”

我正在使用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;
}

这是我的iOS代码

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

}

1 个答案:

答案 0 :(得分: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;
        }];
    }