Как мне загрузить на этот URL-адрес GooglePython

Программы на Python
Ответить
Anonymous
 Как мне загрузить на этот URL-адрес Google

Сообщение Anonymous »

API возвращает мне этот случайный URL-адрес корзины Google при попытке загрузить в нее файлы большего размера:
https://storage.googleapis.com/...-stor ... -goog-hash

Я пытался добавить к нему PUT, я даже пытался предоставить ему хост, x-goog-content-length-range, x-goog- хэш для каждого фрагмента, когда я их просматриваю, но он продолжает возвращать одну и ту же ошибку, что бы я ни делал:
host:storage.googleapis.com
x-goog-content-length-range:0,16383
x-goog-hash:crc32c=ZVBtjQ==

host;x-goog-content-length-range;x-goog-hash
UNSIGNED-PAYLOAD

Мой код:
def calculate_crc32c(data: bytes):
return base64.b64encode(crc32c(data).to_bytes(4, 'big')).decode('utf-8')

def put_file(api: 'API', file: 'File', data: bytes):
checksum_bs4 = calculate_crc32c(data)
content_length = len(data)

position = 0

def file_chunk_generator(chunk_size=16384):
nonlocal sync_event, data, content_length, position
for i in range(0, len(data), chunk_size):
chunk = data[i:i + chunk_size]
yield chunk

with httpx.Client(http2=False) as request:
response = request.put(
FILES_URL.format(api.document_storage_uri, file.hash),
content=file_chunk_generator(),
headers=(headers := {
**api.session.headers,
'content-length': str(content_length),
'content-type': 'application/octet-stream',
'x-goog-hash': f'crc32c={checksum_bs4}',
})
)

if response.status_code == 302:
position = 0
print("Full google upload detected, continuing")
url = response.headers.get("Location")
print(url)
with httpx.Client(http2=False) as request:

for chunk in file_chunk_generator():
print(f'{position},{position + len(chunk) - 1}')
response = request.put(
url,
content=chunk,
headers={
'host': 'storage.googleapis.com',
'x-goog-content-length-range': f'{position},{(position + len(chunk) -1)}',
'x-goog-hash': f'crc32c={calculate_crc32c(chunk)}',
'Content-Length': str(len(chunk)),
}
)
if response.status_code != 200:
break
else:
print("SUCCESS!!!")

if response.status_code != 200:
raise Exception(f"Put file failed - {response.status_code}\n{response.text}")
else:
print(file.uuid, "uploaded")


Подробнее здесь: https://stackoverflow.com/questions/791 ... google-url
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»