【发布时间】:2020-08-24 21:47:18
【问题描述】:
我正在尝试使用 python 将 csv 文件从谷歌云 gcs 存储桶发送到远程 sftp 位置。
import pysftp
from google.cloud import storage
from google.cloud.storage import Blob
client = storage.Client()
bucket = client.bucket("bucket_path")
blob = bucket.blob("FILE.csv")
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host='remote_server', username='user', password='password',
port=22,
cnopts=cnopts) as sftp:
print("Connection succesfully established ... ")
remote_file=sftp.open('remote_location/sample.csv', 'w+')
blob.download_to_file(remote_file)
我收到以下错误:
Connection succesfully established ...
Traceback (most recent call last):
File "/dirvenv/lib/python3.8/site-packages/google/cloud/storage/blob.py", line 997, in download_to_file
self._do_download(
File "/dirvenv/lib/python3.8/site-packages/google/cloud/storage/blob.py", line 872, in _do_download
response = download.consume(transport, timeout=timeout)
File "/dirvenv/lib/python3.8/site-packages/google/resumable_media/requests/download.py", line 168, in consume
self._process_response(result)
File "/dirvenv/lib/python3.8/site-packages/google/resumable_media/_download.py", line 185, in _process_response
_helpers.require_status_code(
File "/dirvenv/lib/python3.8/site-packages/google/resumable_media/_helpers.py", line 106, in require_status_code
raise common.InvalidResponse(
google.resumable_media.common.InvalidResponse: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PARTIAL_CONTENT: 206>)
在处理上述异常的过程中,又发生了一个异常:
Traceback (most recent call last):
File "/dirPycharmProjects/leanplum/file_ftp.py", line 15, in <module>
blob.download_to_file(remote_file)
File "/dirvenv/lib/python3.8/site-packages/google/cloud/storage/blob.py", line 1008, in download_to_file
_raise_from_invalid_response(exc)
File "/dirvenv/lib/python3.8/site-packages/google/cloud/storage/blob.py", line 3262, in _raise_from_invalid_response
raise exceptions.from_http_status(response.status_code, message, response=response)
google.api_core.exceptions.NotFound: 404 GET https://storage.googleapis.com/download/storage/v1/b/gs://bucket_name/o/FILE.csv?alt=media: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PARTIAL_CONTENT: 206>)
Process finished with exit code 1
有什么建议吗?
【问题讨论】:
-
404 表示找不到对象。确保您正确指定了存储桶和对象名称。通过添加
blob.download_to_filename(destination_file_name)这一行blob = bucket.blob("FILE.csv")之后下载对象进行测试 -
Neo Anderson 重新格式化了错误。现在我可以看到您指定的存储桶名称错误。使用实际值编辑您的问题。
-
用斜杠删除存储桶名称后,我得到的问题是: Traceback(最近一次调用最后一次):文件“/PycharmProjects/leanplum/file_ftp.py”,第 20 行,在
blob.download_to_filename (remote_file)文件“/venv/lib/python3.8/site-packages/google/cloud/storage/blob.py”,第1077行,在download_to_filename中,open(filename,“wb”)作为file_obj:TypeError:预期的str ,字节或 os.PathLike 对象,而不是 SFTPFile。有什么建议吗? -
@JohnHanley 是的,我在存储桶名称上错了,我现在可以下载文件,但我无法对远程执行 sftp,因为我收到以下错误:文件“/Users/prithwiraj_samanta/venv/ lib/python3.8/site-packages/google/cloud/storage/blob.py",第 1077 行,在 download_to_filename 中,open(filename, "wb") as file_obj: TypeError: expected str, bytes or os.PathLike object,不是 SFTP 文件。有什么建议吗?
-
您有最新版本的 Google API 吗?
Blob.download_to_fileclaims to support file-like objects.
标签: python google-cloud-platform sftp bucket