Request checksum from Amazon S3 in iOS

折月煮酒 提交于 2019-12-22 10:04:48

问题


I have developed an iOS application which can upload larger videos to an Amazon S3 server

In order to do that, I have used :

  • 1 Amazon V1 API
  • 2 NSURLSession to upload videos to support background uploads .

I have integrated a MD5 checksum to my put request to in-order to validate after file uploaded to Amazon server.

 NSString *md5 = [FileHash md5HashOfFileAtPath:[url path]];

    NSMutableData *commandToSend= [[NSMutableData alloc] init];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [md5 length]/2; i++) {
        byte_chars[0] = [md5 characterAtIndex:i*2];
        byte_chars[1] = [md5 characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [commandToSend appendBytes:&whole_byte length:1];
    }


    s3PutObjectRequest.cannedACL = [S3CannedACL publicRead];
        s3PutObjectRequest.endpoint = s3Client.endpoint;
        s3PutObjectRequest.contentType = fileMIMEType([url absoluteString]);

        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setValue:[NSString stringWithFormat:@"%d",footage.footageId] forKey:@"x-amz-meta-footageId"];
        [dict setValue:[NSString stringWithFormat:@"%d",footage.clipDuration]  forKey:@"x-amz-meta-duration"];
        s3PutObjectRequest.metadata =dict;
        [s3PutObjectRequest configureURLRequest];
         s3PutObjectRequest.contentMD5 = base64EncodedString;


        NSMutableURLRequest *request = [s3Client signS3Request:s3PutObjectRequest];
        NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc]initWithURL:request.URL];
        [request2 setAllHTTPHeaderFields:[request allHTTPHeaderFields]];
        [request2 setValue:nil forHTTPHeaderField:@"Content-Length"];

    [request2 setValue:base64EncodedString forHTTPHeaderField:@"Content-MD5"];

Now I want to validate the checksum after the file has been uploaded to server. I need to know what is the Amazon API that I can get the checksum after upload to server.

I've read below article too and it says we can measure the checksum for the data integrity.

Which object gives the checksum after file uploaded?

Update

S3PutObjectResponse eTag function will give you md5 hash of the uploaded file , but now it gives me an error when i try to print value

"ErrorCode:BadDigest, Message:The Content-MD5 you specified did not match what we received."

来源:https://stackoverflow.com/questions/29847564/request-checksum-from-amazon-s3-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!