Google Cloud Storage提供ServiceUnavailable:503异常后端错误

时间:2019-05-16 02:54:35

标签: google-cloud-platform google-cloud-storage

我正在尝试将文件上传到Google Cloud Storage Bucket。在公开发布的同时,我断断续续地从Google得到了这个例外。该错误几乎每20次上传一次。 google.api_core.exceptions.ServiceUnavailable: 503 GET https://www.googleapis.com/storage/v1/b/bucket_name/o/folder_name%2FPolicy-APP-000456384.2019-05-16-023805.pdf/acl: Backend Error

我正在使用python3,并尝试将google-cloud-storage的版本更新为1.15.0,但这没有帮助。


class GoogleStorageHelper:
    def __init__(self, project_name):
        self.client = storage.Client(project=project_name)

    def upload_file(self, bucket_name, file, file_name, content_type, blob_name, is_stream):
        safe_file_name = self.get_safe_filename(file_name)
        bucket = self.client.bucket(bucket_name)
        blob = bucket.blob(safe_file_name)
        if is_stream:
            blob.upload_from_string(file, content_type=content_type)
        else:
            blob.upload_from_filename(file, content_type=content_type)
        blob.make_public() // Getting Error here
        url = blob.public_url
        if isinstance(url, six.binary_type):
            url = url.decode('utf-8')
        logger.info('File uploaded, URL: {}'.format(url))
        return url

    @staticmethod
    def get_safe_filename(file_name):
        basename, extension = file_name.rsplit('.', 1)
        return '{0}.{1}.{2}'.format(basename, datetime.now().strftime('%Y-%m-%d-%H%M%S'), extension)

您是否遇到过此类问题并已解决?还是有解决此问题的想法?

2 个答案:

答案 0 :(得分:3)

这是最近使用Python make_public()方法的GCS的已知问题。现在,GCS团队正在解决该问题。

作为快速缓解策略,我建议启用重试。 documentation可能有助于设置重试处理策略。

答案 1 :(得分:0)

这有点棘手。我遇到了同样的问题,并发现Python API客户端未启用对upload_from_string()方法的重试。

所有upload_from_string()都调用了upload_from_file()方法,该方法具有重试功能,但实现忽略重试。

def upload_from_string(self, 
                       data, 
                       content_type="text/plain", 
                       client=None, 
                       predefined_acl=None):
    data = _to_bytes(data, encoding="utf-8")
    string_buffer = BytesIO(data)
    self.upload_from_file(
        file_obj=string_buffer,
        size=len(data),
        content_type=content_type,
        client=client,
        predefined_acl=predefined_acl,
    )    

您可以通过使用upload_from_file()实现,并添加重试功能来破解upload_from_string()方法:

from google.cloud._helpers import _to_bytes
from io import BytesIO
from google.cloud.storage import Blob

def upload_from_string(
    data, file_path, bucket, client, content_type, num_retries
):
    data = _to_bytes(data, encoding="utf-8")
    string_buffer = BytesIO(data)    

    blob = Blob(file_path, bucket)

    blob.upload_from_file(
        file_obj=string_buffer,
        size=len(data),
        client=client,
        num_retries=num_retries,
        content_type=content_type
    )