Ios 使用customerKey上传AWS图像

Ios 使用customerKey上传AWS图像,ios,objective-c,amazon-web-services,Ios,Objective C,Amazon Web Services,我使用AWS作为第三方。我想用客户密钥设置服务器端加密。我正在使用以下代码,但总是收到消息“计算出的密钥MD5哈希与提供的哈希不匹配。” 我正在使用客户密钥=“abcdef” 这是我的iOS代码 -(void)uploadAsset:(UIImage*)image filePath:(NSString*)filePath key:(NSString*)customerKey success:(successCallback)success andfailure:(failureCallback

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

我正在使用客户密钥=“abcdef”

这是我的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;
}];

}

对于上载带有密钥的文件,我们必须注意密钥长度为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;
        }];
    }

对于上载带有密钥的文件,我们必须注意密钥长度为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;
        }];
    }