如何使用python在Google云存储上上传bucket中的文件snippets.py?

2024-06-11 17:07:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习如何在Google云存储上上传文件。 我找到了这个代码:https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/storage/cloud-client 这可以完成创建bucket、删除bucket、list、upload、download等一系列操作

但是,我无法运行代码。在

我试过了:

python snippets.py [-h] scene.appspot.com list

错误:

error: argument command: invalid choice: 'scene-maker.appspot.com' (choose from 'create-bucket', 'delete-bucket', 'get-bucket-labels', 'add-bucket-label', 'remove-bucket-label', 'list', 'list-with-prefix', 'upload', 'download', 'delete', 'metadata', 'make-public', 'signed-url', 'rename', 'copy')

我试过了:

^{pr2}$

错误:

error: unrecognized arguments: scene.appspot.com
error: unrecognized arguments: gs://scene.appspot.com

我试过了:

python snippets.py list [-h] scene.appspot.com

错误:

error: argument command: invalid choice: '[-h]' (choose from 'create-bucket', 'delete-bucket', 'get-bucket-labels', 'add-bucket-label', 'remove-bucket-label', 'list', 'list-with-prefix', 'upload', 'download', 'delete', 'metadata', 'make-public', 'signed-url', 'rename', 'copy')

我试过了:

python snippets.py [-h] list

错误:

Traceback (most recent call last):
  File "snippets.py", line 322, in <module>
    list_blobs(args.bucket_name)
  File "snippets.py", line 88, in list_blobs
    bucket = storage_client.get_bucket(bucket_name)
  File "C:\Anaconda2\lib\site-packages\google\cloud\storage\client.py", line 172, in get_bucket
    bucket = Bucket(self, name=bucket_name)
  File "C:\Anaconda2\lib\site-packages\google\cloud\storage\bucket.py", line 113, in __init__
    name = _validate_name(name)
  File "C:\Anaconda2\lib\site-packages\google\cloud\storage\_helpers.py", line 39, in _validate_name
    'Bucket names must start and end with a number or letter.')
ValueError: Bucket names must start and end with a number or letter.

当我运行时:gsutil ls 我得到:

gs://scene.appspot.com/
gs://staging.scene.appspot.com/

如何使用python snippets.py命令? 最终,我希望用户能够从web浏览器上传文件到云存储。在


Tags: nameinpycomcloudbucket错误line
1条回答
网友
1楼 · 发布于 2024-06-11 17:07:57

调用正确的命令语法时遇到问题。在

error: argument command: invalid choice: '[-h]'

命令中不应该有[-h]。方括号只是表示可选参数的标准符号。因此,您可以在命令中使用-h(这将指示您请求命令的帮助/用法屏幕,而不是实际尝试执行命令),或者完全不使用它。在

从您引用的页面:

To run this sample:

$ python snippets.py

usage: snippets.py [-h]
                   bucket_name
                   {create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,list-with-prefix,upload,download,delete,metadata,make-public,signed-url,rename,copy}
                   ...

因此,该命令要求第一个参数(在snippets.py之后)是bucket名称,后面是要执行的特定操作。其他操作可能取决于后面的参数。在

您使用的[-h]被解释为一个参数并抛出解析器:

  • 在第一次尝试中,[-h]被解释为bucket名称, scene.appspot.com被解释为操作。但该操作不是有效的,因此出现了错误。在
  • 在第二组尝试中,[-h]被解释为bucket名称,“list”作为操作(这次是有效的),但是您有另一个参数(您试图指定bucket名称),这对于list操作来说是意外的,因此会出现错误。在
  • {{cd1}第三个bucket中的一个操作被解释为无效的。在
  • 在第四次尝试中,[-h]被解释为bucket名称,“list”表示操作(有效)。这一次,参数和操作的数量看起来是正确的,代码继续进一步验证bucket名称,但由于[-h]不是有效的bucket名称而失败,因此出现了错误。在

所以我尝试一下(我不确定bucket名称的实际预期格式):

python snippets.py scene.appspot.com list
python snippets.py gs://scene.appspot.com list

要将文件上载到bucket(使用从上一次尝试中确定的正确bucket name格式,从现在开始,我假设它是scene.appspot.com),它可能是从以下开始的:

^{pr2}$

要查看此操作的预期语法,请添加-h选项:

python snippets.py -h scene.appspot.com upload

注意:整个方法是命令驱动的,将文件从web浏览器上载到云存储完全是另一回事。我会把它作为一个单独的问题来问,有很多细节,因为有很多答案。在

相关问题 更多 >