如果您对将文件从URL传输到CloudStorage和的文件传输url是有效的感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解将文件从URL传输到CloudStorage的各种细节,并对的文件传
如果您对将文件从URL传输到Cloud Storage和的文件传输url是有效的感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解将文件从URL传输到Cloud Storage的各种细节,并对的文件传输url是有效的进行深入的分析,此外还有关于Cloud Build -> Google Cloud Storage:关于部署时停机的问题、GCS-将文本文件从Google Cloud Storage直接读入python、Google Cloud Code 的 Cloud Run 扩展将 Docker 镜像存储在 Cloud Storage 而不是 Artifact Registry、Google Cloud Storage 对文件名的要求的实用技巧。
本文目录一览:- 将文件从URL传输到Cloud Storage(的文件传输url是有效的)
- Cloud Build -> Google Cloud Storage:关于部署时停机的问题
- GCS-将文本文件从Google Cloud Storage直接读入python
- Google Cloud Code 的 Cloud Run 扩展将 Docker 镜像存储在 Cloud Storage 而不是 Artifact Registry
- Google Cloud Storage 对文件名的要求
将文件从URL传输到Cloud Storage(的文件传输url是有效的)
我是一名Ruby开发人员,尝试使用Python编写的Google Cloud Functions,并且将远程文件从给定的URL传输到Google Cloud
Storage(GCS)遇到了麻烦。
在等效的RoR应用程序中,我下载到该应用程序的临时存储,然后上传到GSC。
我希望有一种方法可以通过Cloud Function将远程文件简单地“下载”到我的GCS存储桶中。
这是我正在处理一些注释的简化示例,真实的代码从私有API提取URL,但是效果很好,而且不是问题所在。
from google.cloud import storageproject_id = ''my-project''bucket_name = ''my-bucket''destination_blob_name = ''upload.test''storage_client = storage.Client.from_service_account_json(''my_creds.json'')# This works fine#source_file_name = ''localfile.txt''# When using a remote URL I get ''IOError: [Errno 2] No such file or directory''source_file_name = ''http://www.hospiceofmontezuma.org/wp-content/uploads/2017/10/confused-man.jpg''def upload_blob(bucket_name, source_file_name, destination_blob_name): bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_filename(source_file_name)upload_blob(bucket_name, source_file_name, destination_blob_name)
提前致谢。
答案1
小编典典无法直接从URL将文件上传到Google Cloud
Storage。由于您是从本地环境运行脚本,因此要上载的文件内容必须在同一环境中。这意味着url的内容需要存储在内存中或文件中。
基于您的代码的示例展示了如何执行此操作:
选项1
:您可以使用该wget
模块,该模块将获取url并将其内容下载到本地文件中(类似于wget
CLI命令)。请注意,这意味着文件将存储在本地,然后从文件上传。os.remove
上传完成后,我添加了一行以删除文件。
from google.cloud import storageimport wgetimport io, osproject_id = ''my-project''bucket_name = ''my-bucket''destination_blob_name = ''upload.test''storage_client = storage.Client.from_service_account_json(''my_creds.json'')source_file_name = ''http://www.hospiceofmontezuma.org/wp-content/uploads/2017/10/confused-man.jpg''def upload_blob(bucket_name, source_file_name, destination_blob_name): filename = wget.download(source_file_name) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_filename(filename, content_type=''image/jpg'') os.remove(filename)upload_blob(bucket_name, source_file_name, destination_blob_name)
选项2
:使用该urllib
模块,其工作方式与该wget
模块相似,但不是写入文件,而是写入变量。请注意,我在Python3上做了这个示例,如果您打算在Python
2.X中运行脚本,则会有一些差异。
from google.cloud import storageimport urllib.requestproject_id = ''my-project''bucket_name = ''my-bucket''destination_blob_name = ''upload.test''storage_client = storage.Client.from_service_account_json(''my_creds.json'')source_file_name = ''http://www.hospiceofmontezuma.org/wp-content/uploads/2017/10/confused-man.jpg''def upload_blob(bucket_name, source_file_name, destination_blob_name): file = urllib.request.urlopen(source_file_name) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_string(link.read(), content_type=''image/jpg'')upload_blob(bucket_name, source_file_name, destination_blob_name)
Cloud Build -> Google Cloud Storage:关于部署时停机的问题
如何解决Cloud Build -> Google Cloud Storage:关于部署时停机的问题?
- name: ''google/cloud-sdk:alpine''
entrypoint: ''gsutil''
args: [''-m'',''rsync'',''-r'',''-d'',''-p'',''dist/'',''gs://my-site-frontend'']
早上好,上面的代码片段是通过 Google Cloud Build 将我的 VueJS 前端的构建复制到 Google Cloud Storage 存储桶的命令,该存储桶将托管网站。
我的问题很简单:如果任何用户在此部署时(执行上述命令)正在浏览,他是否会在 Cloud Build 复制时注意到任何不一致、停机或类似情况/通过 rsync 同步新文件? 这个任务是否足够无缝?也许用户在访问某个正在复制的文件时会感到有些不一致?我应该改用 Cloud Run 吗?
解决方法
是的,您可能会出现一段时间的不一致(文件已过时或未找到)。最好的解决方案是使用以一致方式包装来源的产品。您可以使用 Cloud Run,但也可以为此使用 App Engine 标准。
这两种解决方案的主要优点是每个版本都是统一的,打包在同一个容器中。像这样,您可以轻松地执行回滚、流量拆分、金丝雀发布、A/B 测试……所有这些事情在 Cloud Storage 中都是不可能的。
GCS-将文本文件从Google Cloud Storage直接读入python
我现在有点傻。我一直在阅读大量文档问题,但我做对了。
我在Google Cloud Storage上有一个文件。它在存储桶“ test_bucket”中。在此存储桶中,有一个文件夹“
temp_files_folder”,其中包含两个文件,一个名为“ test.txt”的.txt文件和一个名为“
test.csv”的.csv文件。这两个文件只是因为我尝试同时使用这两个文件,但是两种方法的结果都是相同的。
文件中的内容是
hejsan
我希望将其读入python的方式与在本地使用
textfile = open("/file_path/test.txt", ''r'')times = textfile.read().splitlines()textfile.close()print(times)
这使
[''hej'', ''san'']
我尝试使用
from google.cloud import storageclient = storage.Client()bucket = client.get_bucket(''test_bucket'')blob = bucket.get_blob(''temp_files_folder/test.txt'')print(blob.download_as_string)
但是它给出了输出
<bound method Blob.download_as_string of <Blob: test_bucket, temp_files_folder/test.txt>>
如何获取文件中的实际字符串?
答案1
小编典典download_as_string
是一种方法,您需要调用它。
print(blob.download_as_string())
您更可能希望将其分配给变量,以便您下载一次,然后可以打印它并对其进行任何其他操作:
downloaded_blob = blob.download_as_string()print(downloaded_blob)do_something_else(downloaded_blob)
Google Cloud Code 的 Cloud Run 扩展将 Docker 镜像存储在 Cloud Storage 而不是 Artifact Registry
如何解决Google Cloud Code 的 Cloud Run 扩展将 Docker 镜像存储在 Cloud Storage 而不是 Artifact Registry?
为什么当我使用 Visual Studio Code 扩展“Cloud Code”来部署 Cloud Run 服务时,它似乎将图像内容存储在 Cloud Storage(通过 Container Registry)中。
我可以让它将图像存储在 Google Cloud Artifact Registry 中吗?
解决方法
我刚刚尝试了这个场景,它对我有用!遵循这些步骤应该可以帮助您前进。
- 在 https://console.cloud.google.com/artifacts 创建工件注册表存储库,并在您的客户端上设置 docker auth 以使用 gcloud 对存储库进行身份验证。您可以找到执行此操作的详细步骤 here。
- 在 Cloud Code 中部署到 Cloud Run 时,您会发现它默认使用 Container Registry 存储库作为“容器映像 URL”,但您可以在此处轻松使用工件注册表存储库。在这里,您可以粘贴您在上一步中创建的存储库名称,并附加一个图像名称。 Here''s a screenshot 我刚刚测试的示例。
Google Cloud Storage 对文件名的要求
在 Google Cloud Storage 中,文件名必须满足以下条件:
- 文件名必须为 Unicode 字符集的 UTF-8 编码。
- 文件名长度必须在 1 到 1024 个字符之间。
- 文件名可以包含斜杠 / 以创建目录结构。
- 文件名不能以斜杠 / 开头。
- 文件名不能包含以下字符:
\0
(null),\
,*
,?
,[,],:
,;
,,
,.
。
因此,连字符 - 是被允许的文件名字符之一,可以作为文件名的开头。
今天的关于将文件从URL传输到Cloud Storage和的文件传输url是有效的的分享已经结束,谢谢您的关注,如果想了解更多关于Cloud Build -> Google Cloud Storage:关于部署时停机的问题、GCS-将文本文件从Google Cloud Storage直接读入python、Google Cloud Code 的 Cloud Run 扩展将 Docker 镜像存储在 Cloud Storage 而不是 Artifact Registry、Google Cloud Storage 对文件名的要求的相关知识,请在本站进行查询。
本文标签: