【问题标题】:can't delete multiple object with blank spaces and Cyrillic in file_name using REST API, google cloud storage无法使用 REST API、谷歌云存储删除文件名中带有空格和西里尔文的多个对象
【发布时间】:2016-08-29 19:39:54
【问题描述】:

我的批量删除代码如下:

  @staticmethod
    def gcs_batch_delete(gcs_file_names):
        logging.debug('Deleting ' + str(len(gcs_file_names)) + ' files.')

        boundary = '===============7330845974216740156=='
        headers = {'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'}
        data = '--' + boundary + '\n'
        template = """Content-Type: application/http

DELETE /storage/v1/b/%s/o/""" % os.environ['GCS_BUCKET_NAME']
        bodies = [template + urllib.quote_plus(filename) + '\n' for filename in gcs_file_names]
        data += ('\n--' + boundary + '\n').join(bodies)
        data += '\n--' + boundary + '\n'

        logging.debug('Request:\n' + data)

        url = 'https://www.googleapis.com/batch'
        result = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=headers)

        logging.debug('urlfetch result: ' + str(result.content))

所以,当我删除多个对象时,它只会删除一个带有拉丁字符和文件的文件,它们的名称中没有空格。但是,网站上的文件也会消失,但不会从谷歌云存储中消失。

伙计们,你能帮我升级这个代码吗? :3

【问题讨论】:

    标签: python google-app-engine google-cloud-storage fileserver


    【解决方案1】:

    urllib.quote_plus 会将空格替换为加号,但“+”是 Google Cloud Storage 对象名称的有效字符,因此这不是您想要的。

    尝试使用百分比转义(对于空格,即 %20)。您可以使用 urllib.pathname2url 来执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2017-05-15
      相关资源
      最近更新 更多