How to get the md5sum of a file on Amazon's S3

前端 未结 11 2166

If I have existing files on Amazon\'s S3, what\'s the easiest way to get their md5sum without having to download the files?

Thanks

11条回答
  •  鱼传尺愫
    2020-12-01 01:22

    This works for me. In PHP, you can compare the checksum between local file e amazon file using this:

    
    
        // get localfile md5
        $checksum_local_file = md5_file ( '/home/file' );
    
        // compare checksum between localfile and s3file    
        public function compareChecksumFile($file_s3, $checksum_local_file) {
    
            $Connection = new AmazonS3 ();
            $bucket = amazon_bucket;
            $header = $Connection->get_object_headers( $bucket, $file_s3 );
    
            // get header
            if (empty ( $header ) || ! is_object ( $header )) {
                throw new RuntimeException('checksum error');
            }
            $head = $header->header;
            if (empty ( $head ) || !is_array($head)) {
                throw new RuntimeException('checksum error');
            }
            // get etag (md5 amazon)
            $etag = $head['etag'];
            if (empty ( $etag )) {
                throw new RuntimeException('checksum error');
            }
            // remove quotes
            $checksumS3 = str_replace('"', '', $etag);
    
            // compare md5
            if ($checksum_local_file === $checksumS3) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
    
    

提交回复
热议问题