本文介绍了Google云端存储:CRC32C和Google Play中的不匹配MD5,同时将字符串上传到GCS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试上传JSON字符串并覆盖GCS存储桶中的现有对象时,出现以下错误.

While trying to upload the JSON string and overwrite the existing object in the GCS bucket, getting the below error.

google.api_core.exceptions.BadRequest: 400 POST https://storage.googleapis.com/upload/storage/v1/b/cc-freshdesk/o?uploadType=multipart: {
  "error": {
    "code": 400,
    "message": "Provided CRC32C \"i8Z/Pw==\" doesn't match calculated CRC32C \"mVn0oQ==\".",
    "errors": [
      {
        "message": "Provided CRC32C \"i8Z/Pw==\" doesn't match calculated CRC32C \"mVn0oQ==\".",
        "domain": "global",
        "reason": "invalid"
      },
      {
        "message": "Provided MD5 hash \"6NMASNWhbd4WlIj/tWK4Sw==\" doesn't match calculated MD5 hash \"9H5THzsUBARmhzw5NjjgNw==\".",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}
: ('Request failed with status code', 400, 'Expected one of', <HTTPStatus.OK: 200>)

找到以下代码段:

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
config_blob = bucket.blob(destination_blob_name)
config_blob.upload_from_string(json.dumps(config_data,indent=4), content_type='text/plain')

任何人都可以帮助我了解为什么可能会发生此问题.

Can anyone help me understand why this issue might be occurring.

推荐答案

要复制您遇到的错误:

import json
from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket('some-bucket')

# blob1 object
blob1 = bucket.get_blob('file.json')

# downloads content
blob1_string = blob1.download_as_string()

# converts to dict and update content
blob1_obj = json.loads(blob1_string)
blob1_obj['some-key'] = 'some value'

# upload using same blob instance
blob1.upload_from_string(json.dumps(blob1_obj))

# throws error like this `Provided MD5 hash "Ax9olGoqOSb7Nay2LNkCSQ==\" #doesn't match calculated MD5 hash \"XCMPR0o7NdgmI5zN1fMm6Q==\".",

您可能正在使用相同的Blob来下载和上传内容.为防止出现此错误,您需要创建两个blob实例:

You're probably using the same blob to download and upload contents. To prevent this error you need to create two instances of blob:

import json
from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket("some-bucket")

# blob1 object -- for downloading contents
blob1 = bucket.get_blob('file.json')

blob1_string = blob1.download_as_string()
# Convert to dictionary
blob1_obj = json.loads(blob1_string)
# Add stuff
blob1_obj['some-key'] = 'some value'

# blob2 object -- for uploading contents
blob2 = bucket.get_blob('file.json')

blob2.upload_from_string(json.dumps(blob1_obj))

# no error

这篇关于Google云端存储:CRC32C和Google Play中的不匹配MD5,同时将字符串上传到GCS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:37