简体   繁体   中英

Trying to upload a .wav file to a bucket using python-requests

I'm trying to upload a .wav file (lets say test.wav ) into my google storage bucket but i'm running into some problems: A storage object gets uploaded with the appropriate 'test.wav' name, but inside it is just the data from my request. Also the contentType in the bucket is displayed as application/x-www-form-urlencoded .

My bucket has public read/write/delete permissions and uploading other file types works fine. Uploading this way also works fine through postman.


url = "https://www.googleapis.com/upload/storage/v1/b/<bucket_name>/o"

payload ={
    "acl":"public-read",
    "file":open('test.wav'),
    "signature":signature,
    "policy":policy              
}
headers={"contentType":"audio/wav"}
params={"uploadType":"media","name":"test.wav"}
response = requests.post(url,data=payload,headers=headers,params=params)
print(response.text)

Currently I get the following error :

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 
4420: character maps to <undefined>

I've also tried scipy.io.wavfile.read() and pydub.AudioSegment() but these don't work for me either.

Ideally, I'd want the file to be successfully uploaded and usable for transcription through google's STT.

Thanks and regards.

Found a workaround to this problem. Instead of using the requests module, I've switched over to using the method shown here .

Doing this uploads the file properly, instead of its data being uploaded to a file with a .wav extension. Thereby fixing my issue.

You're specifying the uploadType parameter to be media . That means that the body of the request is the body of the object you're uploading. However, you're specifying the body field of your POST to be a dictionary with fields like ""acl", "signature", and "file". It looks like maybe you're attempting a sort of form-style POST, but that's not how media uploads look.

Here's how you'd use Requests to do a media-style upload to GCS:

import requests

url = "https://www.googleapis.com/upload/storage/v1/b/<bucket_name>/o"

headers = {
    "acl": "public-read",
    "Authorization": "Bearer ...",
    "Content-Type": "audio/wav",
}
params = {"uploadType": "media", "name": "test.wav"}
with open('test.wav', 'rb') as file:
  r = requests.post(url, params=params, headers=headers, data=file)
print(r.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM