Usei o código a seguir para fazer upload do arquivo para o armazenamento do GCP. Recebi um erro de tempo limite. O arquivo tem cerca de 1 GB. É um arquivo relativamente grande. Como resolver esse problema de tempo limite de upload?
Arquivo "/Users/xxxx/opt/anaconda3/lib/python3.8/site-packages/google/resumable_media/requests/_request_helpers.py", linha 136, em http_request return _helpers.wait_and_retry (func, RequestsMixin._get_status_code, retry_stry) Arquivo "/Users/xxxx/opt/anaconda3/lib/python3.8/site-packages/google/resumable_media/_helpers.py", linha 186, em wait_and_retry elevar erro Arquivo "/ Users / xxx / opt / anaconda3 / lib / python3.8 / site-packages / google / resumable_media / _helpers.py ", linha 175, em wait_and_retry response = func () File" /Users/xxxx/opt/anaconda3/lib/python3.8/site-packages/google/ auth / transport / requests.py ", linha 482, em request response = super (AuthorizedSession, self) .request (File" /Users/xxxxx/opt/anaconda3/lib/python3.8/site-packages/requests/sessions. py ", linha 542, na solicitação resp = self.send (prep,** send_kwargs) File "/Users/xxxxx/opt/anaconda3/lib/python3.8/site-packages/requests/sessions.py", linha 655, em send r = adapter.send (request, ** kwargs) File "/Users/xxxxxx/opt/anaconda3/lib/python3.8/site-packages/requests/adapters.py", linha 498, em enviar aumento ConnectionError (err, request = request) requests.exceptions.ConnectionError: ('Connection abortado. ', tempo limite (' Tempo limite da operação de gravação esgotado '))A operação de gravação atingiu o tempo limite '))A operação de gravação atingiu o tempo limite '))
def upload_file_to_gcs(local_filepath:str, bucket_name:str, gcs_filepath:str = None):
if local_filepath == None:
raise ValueError("local_filepath cannot be None")
if not os.path.isfile(local_filepath) or not os.path.exists(local_filepath):
raise TypeError(f"{local_filepath} is not a file or does not exist.")
if bucket_name == None:
raise ValueError("bucket cannot be None")
if not bucket_exist(bucket_name):
logging.info(f"Bucket {bucket_name} does not exist. Creating...")
create_bucket(bucket_name)
logging.info(f"Uploading {local_filepath} to GCS...")
# Initialise a client
storage_client = storage.Client()
if gcs_filepath == None:
gcs_filepath = Path(local_filepath).name
#create bucket object
bucket = storage_client.get_bucket(bucket_name)
#upload
blob = bucket.blob(gcs_filepath)
uploaded_file = blob.upload_from_filename(local_filepath)
logging.info(f"Uploaded {local_filepath} to {bucket_name} in GCS.")
return vars(blob)
Você pode definir um tempo limite ao criar o cliente do intervalo. Dê uma olhada em - https://googleapis.dev/python/storage/latest/retry_timeout.html
Se você tiver uma conexão de Internet ruim, também pode brincar com o tamanho do bloco dos uploads (embora não seja recomendado)
from google.cloud import storage
storage.blob._DEFAULT_CHUNKSIZE = 5 * 1024* 1024 # 5 MB
storage.blob._MAX_MULTIPART_SIZE = 5 * 1024* 1024 # 5 MB
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras