使用 Python 在 SFTP 服务器上保存时截断文件? [英] Truncating the file while saving on SFTP server using Python?
问题描述
这是我们可以使用 GCP Cloud 功能将数据从 Google 云存储发送到 SFTP 服务器吗?
with pysftp.Connection(host=myHostName, username=myUsername,
password=myPassword, cnopts=cnopts) as sftp:
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(filename) #source_blob_name
with sftp.open(remotepath, 'w+', 32768) as f:
blob.download_to_file(f)
W+
/w
命令不会截断前一个文件并抛出错误.这是当 SFTP 服务器中已经存在同名文件时!对此的最佳解决方案应该是什么?
The W+
/w
command is not truncating the previous file and throwing error. This is when there is already a file with same name in SFTP server existing! what should be the optimal solution to this?
完整的错误代码日志是
gcs-to-sftp
4wz44jq3oe2g
Traceback (most recent call last):
File "/env/local/lib/python3.7/site-packages/google/cloud/storage/client.py", line 728, in download_blob_to_file
checksum=checksum,
File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 986, in _do_download
response = download.consume(transport, timeout=timeout)
File "/env/local/lib/python3.7/site-packages/google/resumable_media/requests/download.py", line 168, in consume
self._process_response(result)
File "/env/local/lib/python3.7/site-packages/google/resumable_media/_download.py", line 186, in _process_response
response, _ACCEPTABLE_STATUS_CODES, self._get_status_code
File "/env/local/lib/python3.7/site-packages/google/resumable_media/_helpers.py", line 104, in require_status_code
*status_codes
google.resumable_media.common.InvalidResponse: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PARTIAL_CONTENT: 206>)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 449, in run_background_function
_function_handler.invoke_user_function(event_object)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 268, in invoke_user_function
return call_user_function(request_or_event)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 265, in call_user_function
event_context.Context(**request_or_event.context))
File "/user_code/main.py", line 20, in hello_sftp
copy_file_on_ftp(myHostName, myUsername, myPassword, bucket_name, filename)
File "/user_code/main.py", line 42, in copy_file_on_ftp
file_to_export.download_to_file(f)
File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 1128, in download_to_file
checksum=checksum,
File "/env/local/lib/python3.7/site-packages/google/cloud/storage/client.py", line 731, in download_blob_to_file
_raise_from_invalid_response(exc)
File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 4100, 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/sftp__test/o/test_file.csv?alt=media: No such object: sftp__test/test_file.csv: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PARTIAL_CONTENT: 206>)
推荐答案
w+
模式下的 +
不应该存在.它打开文件进行读取和写入,这在您的场景中是不需要的.
The +
in w+
mode should not be there. It opens the file for both reading and writing, what is not needed in your scenario.
虽然 w
应该截断文件(它在常见的 OpenSSH Linux 服务器上确实如此),但当与 +
.
Although the w
should truncate the file (and it does on the common OpenSSH Linux server), it does not seem to work with your server, when combined with the +
.
这篇关于使用 Python 在 SFTP 服务器上保存时截断文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!