2017-11-16 9 views
0

Google Cloud Storage의 버킷에 파일을 업로드하는 방법을 배우려고합니다. 는이 코드를 발견 https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/storage/cloud-client 이 내가 코드를 실행할 수 없습니다 오전, 운영의 무리가 만든 버킷을 좋아합니까 삭제 - 버킷리스트 ...python snippets.py를 사용하여 Google Cloud Storage의 버킷에 파일을 업로드하는 방법은 무엇인가요?

을 업로드 및 다운로드 그러나 수 있습니다.

내가 시도 :

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')

내가 시도 :

python snippets.py [-h] list scene.appspot.com 
python snippets.py [-h] list gs://scene.appspot.com 
python snippets.py [-h] list "gs://scene.appspot.com" 
python snippets.py [-h] list bucket_name 

오류 :

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 명령을 사용할 수 있나요? 궁극적으로 사용자가 웹 브라우저에서 Cloud Storage로 파일을 업로드 할 수 있기를 바랍니다.

답변

1

올바른 명령 구문을 호출하는 데 문제가 있습니다.

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 후) 첫 번째 인수가 수행 할 특정 작업에 의해 다음 버킷 이름이 될 것으로 기대하고있다. 작업에 따라 다른 인수가 나타날 수 있습니다.

[-h] 인수로 해석됩니다 사용하고 파서를 던졌습니다 : 1 시도 [-h]에서
  • 는 버킷 이름과 동작으로 scene.appspot.com로 해석됩니다. 하지만 그 작업은 유효하지 않습니다 - 따라서 오류.
  • 두 번째 시도 그룹에서 [-h]은 버킷 이름으로 해석되며 'list'는 작업으로 (이번에는 유효한 하나), 예상치 못한 또 다른 인수 (버킷 이름을 지정하려는 시도)가 있습니다. list 작업 - 따라서 오류.
  • 세 번째 시도에서 list은 버킷 이름으로 해석되고 [-h]이 연산으로 - 다시 잘못된 것입니다. 따라서 오류가 발생합니다.
  • 네 번째 시도에서 [-h]은 버킷 이름으로 해석되고, 'list'는 연산 (유효한)으로 해석됩니다. 이번에는 인수의 개수와 연산이 정확 해지고 코드는 버킷 이름의 유효성을 검사하기 위해 더 진행되며 [-h]은 유효하지 않으므로 실패합니다. 따라서 오류가 발생합니다.

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

    이 (버킷에 파일을 업로드에서 결정 올바른 버킷 이름 형식을 사용하려면 :

그래서 (내가 버킷 이름에 대한 실제 예상되는 형식에 대한 확실 해요)하려고 할 것

python snippets.py scene.appspot.com upload 

이 작업의 예상 구문을 참조 -h 옵션을 추가하려면 : 이전 시도는 I로 시작, 아마 뭔가)는 지금부터 scene.appspot.com있어 가정합니다

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

:이 모든 방법은 명령 구동되고, 클라우드 스토리지에 웹 브라우저에서 파일을 업로드하는 것은 완전히 다른 이야기입니다. 저는 그것에 대한 많은 해답이있을 수 있기 때문에 많은 세부 사항으로 별도의 질문으로 물어볼 것입니다.

+0

작동합니다! 그런 세부적인 설명에 감사드립니다. – Beginner