Skip to content

s3: Multipart files not checked or syncronized based on Checksum (ETag, additional headers) #523

Closed

Description

@robbat2

Multipart uploads to S3 do not have working checksum-based synchronization or validations. This can be implemented in two different ways:

  1. Set additional headers of the known checksums of the file (as a bonus, you can set multiple different checksums on a single file
  2. If the chunk size is consistent, the local ETag can be calculated for comparison. The chunk size can also be stored as a header, to ensure later calculations can be repeated, This is possible as documented here: http://www.perlmonks.org/?node_id=1140988

What is your rclone version (eg output from rclone -V)
rclone v1.29

Which OS you are using and how many bits (eg Windows 7, 64 bit)
Gentoo Linux, 64-bit

Which cloud storage system are you using? (eg Google Drive)
S3 (Ceph Hammer)

The command you were trying to run (eg rclone copy /tmp remote:tmp)
rclone sync . $REMOTE_NAME:$BUCKET_NAME/ --checksum

A log from the command with the -v flag (eg output from rclone -v copy /tmp remote:tmp)

rclone  sync .  $REMOTE_NAME:$BUCKET_NAME/      --checksum --dump-headers --dump-bodies
2016/06/14 17:09:27 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2016/06/14 17:09:27 HTTP REQUEST
2016/06/14 17:09:27 HEAD /$BUCKET_NAME HTTP/1.1
Host: objects-us-west-1.dream.io
User-Agent: rclone/v1.29
Authorization: AWS XXXXXX:Ke4mVHLXl4WUSPoyfVYZ6LoPFiM=
Date: Wed, 15 Jun 2016 00:09:27 UTC

2016/06/14 17:09:27 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2016/06/14 17:09:27 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2016/06/14 17:09:27 HTTP RESPONSE
2016/06/14 17:09:27 HTTP/1.1 200 OK
Date: Wed, 15 Jun 2016 00:09:27 GMT
X-Amz-Request-Id: tx000000000000001bbb918-0057609cb7-d8d7311-default
X-Rgw-Bytes-Used: 44040192
X-Rgw-Object-Count: 3
Content-Length: 0

2016/06/14 17:09:27 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2016/06/14 17:09:27 S3 bucket robjoh84-congress-test2: Building file list
2016/06/14 17:09:27 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2016/06/14 17:09:27 HTTP REQUEST
2016/06/14 17:09:27 GET /$BUCKET_NAME?delimiter=&max-keys=1024&prefix= HTTP/1.1
Host: objects-us-west-1.dream.io
User-Agent: rclone/v1.29
Authorization: AWS XXXXXXXX:lEMCCAZstZ5rJbzqUt25R59v5j8=
Date: Wed, 15 Jun 2016 00:09:27 UTC
Accept-Encoding: gzip

2016/06/14 17:09:27 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2016/06/14 17:09:27 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2016/06/14 17:09:27 HTTP RESPONSE
2016/06/14 17:09:27 HTTP/1.1 200 OK
Content-Length: 525
Content-Type: application/xml
Date: Wed, 15 Jun 2016 00:09:27 GMT
X-Amz-Request-Id: tx000000000000001bbb91b-0057609cb7-d8d7311-default

$BUCKET_NAME1024falsetest.mem2016-06-15T00:01:37.000Z"4388daee7926e0b260eaf1bb51bdcb35-7"33554432STANDARDrobjoh84$UID

2016/06/14 17:09:27 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2016/06/14 17:09:27 S3 bucket $BUCKET_NAME: Waiting for checks to finish
2016/06/14 17:09:27 Waiting for deletions to finish
2016/06/14 17:09:27 S3 bucket $BUCKET_NAME: Waiting for transfers to finish
2016/06/14 17:09:27 S3 bucket $BUCKET_NAME: Waiting for deletes to finish (during+after)

Transferred: 0 Bytes ( 0.00 kByte/s)
Errors: 0
Checks: 1
Transferred: 0
Elapsed time: 400ms

Activity

  1. ncw commented on Jun 15, 2016

    @ncw
    Member

    I prefer option 1 as there is already a precedent for that - that is how b2 does it. That would also make rclone md5sum work and enable checksum based syncing for large files. Option 2 only lets you calcuate the etag when you download the file which is useful, but not as useful for rclone.

    Is there a de-facto standard for the name of the metadata - I try to keep rclone compatible with other tools if possible?

    This could also be implemented for swift (&hubic) - I wonder if there is any standard metadata keys for that also?

  2. ncw commented on Jun 15, 2016

    @ncw
    Member

    I asked on StackOverflow a question about a standard metadata key for storing MD5SUMs.

  3. robbat2 commented on Jun 16, 2016

    @robbat2
    Author

    The closest I've seen to a standard name in S3 tools was x-amz-meta-$HASHNAME. I did want to mention that
    there is another strong advantage to the ETag method, notably parallel checksumming of large files (assuming you have enough disk IO to feed multiple threads of checksumming).

    Maybe support both in headers...

  4. added this to the milestone on Jul 15, 2016
  5. jamshid commented on Jan 9, 2017

    @jamshid

    Option 2 only lets you calcuate the etag when you download the file which is useful, but not as useful for rclone.

    I don't understand this. Using the S3 ETag seems like the better option since it does not require adding a custom header. The S3 ETag is returned in listings, so you don't even have to HEAD the object to validate its checksum against the local file. You certainly don't need to download the file, which would defeat the purpose of .

    You don't need to store the chunk size in a separate header since the number of parts is included in the ETag. The format is "HEX MD5 OF EACH PART'S MD5 - NUMBER OF PARTS". The chunk size is simply the file size / number of parts. with the remainder being the last part.

  6. ncw commented on Jan 9, 2017

    @ncw
    Member

    @jamshid wrote

    I don't understand this. Using the S3 ETag seems like the better option since it does not require adding a custom header.

    Having an actual MD5 of big files would enable cloud to cloud copies from drive to s3 with checking for example, and it would enable rclone md5sum to work, neither of which would work with Option 1.

  7. chris-redekop commented on Oct 25, 2017

    @chris-redekop
    Contributor

    I drafted an implementation of Option 1 for S3. During an update, it detects large files and attaches the MD5 hash as metadata; during a check, it falls back to checking the metadata when the ETag is not an MD5 hash.

    I have a question about this implementation and S3-backed crypt remotes. When S3#Update asks a crypt proxy for its hash, on line chris-redekop@515def7#diff-7db3cb93944d57b2ffc803281c906018R1009 , crypt returns "" (https://github.com/ncw/rclone/blob/master/crypt/crypt.go#L615 ). This r esults in an empty hash being saved to the metadata, instead of the hash of the encrypted file.

    To address this, can crypt remotes implement this Hash function by encrypting the proxied file and then calculating its MD5 hash? Should they? I think this would fix MD5 hashes for large encrypted files on S3, but I'm concerned about unintended consequences.

  8. chris-redekop commented on Nov 26, 2017

    @chris-redekop
    Contributor

    I took a closer look support for ObjectInfo#Hash in crypt::ObjectInfo. I think this would be a great feature, and I think it's doable; but it would need some rework in the hash handshake between file systems. In particular, I think the handshake needs to broker

    1. preferred vs. available hash algorithms,
    2. which side of the handshake picks the preferred algorithm,
    3. the hash calculation of transformed (e.g. encrypted, compressed Split large files #625, etc) streams on-the-fly.

    In the meantime, I added ETag support to the S3 file system. The change is up in #1863.

  9. ncw commented on Nov 28, 2017

    @ncw
    Member

    Sorry @chris-redekop I missed your comment from 25th October.

    I drafted an implementation of Option 1 for S3. During an update, it detects large files and attaches the MD5 hash as metadata; during a check, it falls back to checking the metadata when the ETag is not an MD5 hash.

    That is pretty much exactly how the b2 large file hash works. However it has the downside you've pointed out below which are in #1767

    To address this, can crypt remotes implement this Hash function by encrypting the proxied file and then calculating its MD5 hash? Should they? I think this would fix MD5 hashes for large encrypted files on S3, but I'm concerned about unintended consequences.

    They could, but it is likely to mean the file is encrypted twice and possibly transferred twice so not ideal. See #1767 for more discussion!

    I took a closer look support for ObjectInfo#Hash in crypt::ObjectInfo. I think this would be a great feature, and I think it's doable; but it would need some rework in the hash handshake between file systems. In particular, I think the handshake needs to broker

    I'm not sure how you would solve encrypting and transferring the data twice?

    To solve the crypt hash problem, I was thinking of making a v2 crypt format which stores a blob of metadata in front of each file. This would be hashes in all supported formats, and also the full name of the file. This would mean that it was relatively cheap to look up the hash of an encrypted file.

  10. chris-redekop commented on Nov 29, 2017

    @chris-redekop
    Contributor

    Thanks for the context. #1767 does go over this issue in detail! I'd like to talk through this "calculate the sum in advance" option a bit more, as it addresses my use case better than #1863: "as a backup operator, I want to (crypt) check the large files that rclone copies to S3".

    They could, but it is likely to mean the file is encrypted twice and possibly transferred twice so not ideal. See #1767 for more discussion!

    I do see the need for double encryption--once to calculate the sum in advance and once to transfer the file encrypted. (I don't see a need for a double transfer?) The double encryption seems like a fair tradeoff to me, for four reasons:

    1. My "S3 backup operator with large files" use case needs two encryptions either way:

      1. If we calculate in advance, we need once for the sum and once for the upload, like in Large files uploaded via crypt remote to B2 are missing SHA1SUMs #1767, or
      2. If we calculate later, we need once for the upload and once later for the crypt check. (This assumes S3: Add ETag support - fixes #523 #1863, because S3 otherwise has no support for large-file MD5 sums. See 3 below.)
    2. When I "calculate the sum in advance", I never have to crypt check my backup files. (S3 assured during upload that they match.) This means I avoid re-checking a remote FS to check newly backed-up files, which means I also avoid rechecking the previously backed-up files that I have already checked. In this sense, "calculating the sum in advance" actually reduces the overall number of re-encryptions.

    3. Other than the S3 ETag--which we know doesn't address all use cases--uploading the MD5 sum in advance is the only way to verify large files on S3. There is no other way to confirm that the bytes on S3's side were, e.g., not corrupted during transmission. Note that uploading the MD5 sum later, as arbitrary metadata that S3 does not confirm, does not address this issue. We need S3 to recognize and confirm the sum on its side; and uploading the MD5 sum in advance is currently the only way to achieve this. (If S3 ever adds the ability to do this check afterward, all the better.)

    4. This is Amazon's recommended approach for ensuring data integrity. It works transparently for multi-part upload, and Amazon's CLI does this by default.

    To solve the crypt hash problem, I was thinking of making a v2 crypt format which stores a blob of metadata in front of each file. This would be hashes in all supported formats, and also the full name of the file. This would mean that it was relatively cheap to look up the hash of an encrypted file.

    I like this idea as an optimization. I also think it would be fun to write an S3 crawler to upgrade v1 files to v2! 🕸️

    Functionally, though, it does not address 3 above. I think we need at least one anchor sum that is verified by S3 and and serves to "certify" the other sums in the v2 blob. Otherwise, we can't be sure that the uploaded sums actually match the contents of the uploaded file. For this reason, I still support calculating the sum in advance.

    The unfortunate thing here--touched upon on #1767 --is that the APIs want the sum before the upload instead of after. I don't see this as a reason to not implement the "calculate the sum in advance" functionality; if the APIs improve later, rclone can improve its "calculate the sum in advance" impl right along with them. 😄

    What do you think? I prefer this solution as an alternative to #1863, and would be happy to switch over to this work.

  11. ncw commented on Nov 30, 2017

    @ncw
    Member

    I agree by and large with your points above. I'll just call out some things

    I don't see a need for a double transfer?

    Remember rclone supports remote -> remote transfers so it will involve a double transfer from the source.

    It works transparently for multi-part upload, and Amazon's CLI does this by default.

    I've looked at the docs and Content-MD5 doesn't seem to be supported for multipart uploads. It is supported for each chunk and the AWS Go SDK does that for me already.

    Functionally, though, it does not address 3 above. I think we need at least one anchor sum that is verified by S3 and and serves to "certify" the other sums in the v2 blob.

    Yes that is mostly why I haven't done it....

    What do you think? I prefer this solution as an alternative to #1863, and would be happy to switch over to this work.

    I think that it won't work with multipart files unfortunately :-( Here are the docs for initiate, upload a part and finalise - I'd appreciate a double check on that.


    Rclone goes to some effort to check hash integrity. After uploading something, rclone will immediately check the hash is correct (ie check the Etag returned by S3 matches that caculated locally) and delete the file if it isn't which has the same eventual result as sending it in the Content-MD5 header I think.

    That scheme would work well with #1863 and I think it covers your requirements of having an object whose hash has been checked at the point of upload.

    I think we would have to hide it behind a feature flag --s3-etag or something like that so we don't break backwards compatibility with other workflows.

    Although we could always use this mechanism for uploads - it is just downloads that are potentially problematic.

    In fact looking at the s3manager code it has all the info there (namely MD5SUMs) of all the parts so it would be really easy for it to check the etag returned there...

  12. 4 remaining items

  13. chris-redekop commented on Dec 3, 2017

    @chris-redekop
    Contributor

    Ah, good idea re: submitting a PR to aws-go-sdk. I'll take a closer look and see how that might work.

    Re: trackng moves, yes, I see what you mean. By making hashes available sooner, crypt remotes look more like unencrypted remotes.

  14. ncw commented on Dec 4, 2017

    @ncw
    Member

    I did a little test with rclone uploading an 11M file.

    dd if=/dev/zero of=11M bs=1M count=11
    rclone copy -vv --dump-bodies 11M s3:rclone 2>&1 | tee s3.log
    tr -d '\000' < s3.log > s3-no-nulls.log
    

    s3-no-nulls.log

    If you look at that log you can see

    • each chunk is uploaded with an X-Amz-Content-Sha256 so each chunk is protected
    • to finish the upload, all md5sums of all the chunks are sent - here amazon checks the integrity of the entire upload
      • also here is enough info to calculate what the ETag should be.
    <CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Part><ETag>&#34;5f363e0e58a95f06cbe9bbc662c5dfb6&#34;</ETag><PartNumber>1</PartNumber></Part>
    <Part><ETag>&#34;5f363e0e58a95f06cbe9bbc662c5dfb6&#34;</ETag><PartNumber>2</PartNumber></Part>
    <Part><ETag>&#34;b6d81b360a5672d80c27430f39153e2c&#34;</ETag><PartNumber>3</PartNumber></Part>
    </CompleteMultipartUpload>
    

    So I think s3manager and S3 are ensuring the integrity of large file uploads quite well.

  15. chris-redekop commented on Dec 6, 2017

    @chris-redekop
    Contributor

    Yes, I see what you mean! Thanks for finding this.

    So Content-MD5 aside, the SDK is always SHA-256 hashing its chunked payload. This addresses a big part of (the motivation behind) my use case; all that remains is, "as a backup operator, I want to (crypt) check the large files that rclone copied to S3 some time in the past"

    I do want to address this use case, but now that (I realize that) my rcloned files have been verified 😅 I'm not in as big of a hurry. Given the new SHA-256 information, are you still interested in adding either MD5 or ETag support to S3 large files? In the meantime, I'm going to keep studying the ETag before I propose one or the other.

  16. chris-redekop commented on Dec 8, 2017

    @chris-redekop
    Contributor

    Given what I found below, @ncw, I think calculating and specifying the MD5 ahead of the download is the best approach. (I think the ETag is too fragile.) What do you think?

    1. This API call initially looked promising, as a source of truth for the list of an object's parts. If I am reading the documentation correctly, however, it only applies to in-progress uploads (not completed uploads). I do not see a way to discover upload IDs for completed uploads.
    2. S3 changes the ETag of large objects when copied server-side between buckets, so any multi-part metadata that we upload with an object is of limited use--for ETag-checking local rclone uploads that have not been moved server-side--and is potentially misleading.
  17. ncw commented on Dec 12, 2017

    @ncw
    Member

    Given what I found below, @ncw, I think calculating and specifying the MD5 ahead of the download is the best approach. (I think the ETag is too fragile.) What do you think?

    I think that is probably the best approach and matches the approach taken by b2. I agree with you that the ETag is probably too fragile. I think the major pointer is that direction is that it isn't documented by AWS, so they can change it whenever they like...

    I didn't know about Etag of large object copies. Note that rclone does a server side copy when updating the modtime of a file so it is more common that you might think.

  18. chris-redekop commented on Dec 13, 2017

    @chris-redekop
    Contributor

    Sounds good. I'll close #1863, then, and prep my original branch for a PR.

  19. ncw commented on Dec 14, 2017

    @ncw
    Member

    That would be great :-)

    I note in your branch you use metaMD5Hash = "Md5hash" - I'd like to follow some standard here if there is one! Any thoughts?

  20. chris-redekop commented on Dec 18, 2017

    @chris-redekop
    Contributor

    Leaving a note here: in it's CLI/S3 FAQ, AWS lists Signature Version 4's x-amz-content-sha256 as a preferred alternative to Content-MD5 when uploading files to S3.

  21. ncw commented on Sep 3, 2023

    @ncw
    Member

    We now

    • Add metadata with a calculated MD5 hash on each large object
    • Check the etag returned by s3 for multipart uploads where possible

    I think this is sufficient to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions