Python Forum
HTTP, python size e upload file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP, python size e upload file
#1
Hello everyone,

I need to use the HTTP API of transfernow (https://developers.transfernow.net/) to implement a Python application that allows you to upload files and then store the link
for downloading resources.

The question is in detail how to correctly manage the request in Python by providing the correct size of a file.

The problem is the following, the API requires:
1) initially to perform an HTTP POST (https://developers.transfernow.net/#crea...w-transfer) including the size of the file,
2) subsequently in the workflow there are several calls,
3) then there is an API for the actual upload of the file (https://developers.transfernow.net/#file-parts-upload),
4) subsequently the completion of the upload (https://developers.transfernow.net/#comp...fer-upload).

A very simple example, I create an ASCII file with the simple writing: hello.
If I calculate the size of this file, eg:
os.stat(file_path).st_size

I get the size 5 and this size is passed in the call at point 1

Then at point 3, I perform the actual upload of the file, for example like this:

files = {
"resource": (file_name, open(file_path, "rb"), mimetype)
}

response = requests.request("PUT", upload_url, files=files)
tracing the request I have for example:
< Content-Length: 180
< Content-Type: multipart/form-data; boundary=08fc9fa3aa6283a9ffa2f46d6b8ad13d
<
< --08fc9fa3aa6283a9ffa2f46d6b8ad13d
Content-Disposition: form-data; name="resource"; filename="file1.txt"
Content-Type: text/plain

hello
--08fc9fa3aa6283a9ffa2f46d6b8ad13d--

the problem is the Content-Length: 180 because at step 4 I then receive the following error:
{"error":"File GpjwR4un does not have the right size in s3 (provided: 5, actual: 180","codes":[]}

Do you know how to solve this problem? I think it is impossible to calculate the size at point 1 in 180 as if it were an http call. I think you have to act
at point 3 in order to pass the file with size 5...but I don't know how to do it.

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A working code to upload a file using sftp? Python_FTP 3 1,496 Nov-29-2023, 02:26 PM
Last Post: Pedroski55
  python -m http.server giving invalid syntax echowit 5 7,379 May-17-2018, 02:13 PM
Last Post: echowit
  Python 2 http networking problems, if any Aeneas 1 3,246 Sep-06-2017, 07:24 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020