【发布时间】:2023-08-20 10:24:01
【问题描述】:
我正在尝试从 Google 存储中压缩一些文件。
Python的zipfile在gcloud中没有找到文件,只在项目中。
如何为我的代码找到 gcloud 中的文件?
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
for revenue in revenues:
# queryset with files a lot, so, for a each file, add in zip
t = tempfile.NamedTemporaryFile()
t.write(revenue.revenue.name)
if revenue.revenue.name:
t.seek(0)
with default_storage.open(revenue.revenue.name, "r") as file_data:
zip_file.write(file_data.name, compress_type=zipfile.ZIP_DEFLATED)
# the code dont pass from this part
t.close()
response = HttpResponse(content_type='application/x-zip-compressed')
response['Content-Disposition'] = 'attachment; filename=my_zip.zip'
response.write(zip_buffer.getvalue())
return response
在这一部分中,我编写了从 gcloud 打开的文件,但在函数内部停止:
def write(self, filename, arcname=None, compress_type=None):
"""Put the bytes from filename into the archive under the name
arcname."""
if not self.fp:
raise RuntimeError(
"Attempt to write to ZIP archive that was already closed")
st = os.stat(filename)
# when I try find the file, the command os.stat search in project, not in gcloud
“os.stat(filename)”在项目中搜索文件,如何在gcloud中查找?
【问题讨论】:
-
你是什么意思,for my code find the files in gcloud?这里没有太多代码可以使用。
-
项目树中文件的代码搜索,但我需要在 gcloud 中搜索
-
您能说得更清楚些吗?您是否在 Cloud Storage 存储分区中保存了一些文件并且想要访问它们?您是否有保存在 Cloud Shell 中的文件并且想要访问它们?还是您在个人计算机上使用 Cloud SDK 并且想要获取一些文件?
-
第一个选项。我将文件保存在 gcloud 存储桶中,然后我可以阅读,然后我不能是 zip。我正常读取文件,但是当我压缩它时,Python 的“zipfile”本机在您的本机进程中找不到 gcloud 中的文件,因为它正在搜索项目,而不是 gcloud。
标签: python django gcloud zipfile