私はGoogle Cloud Bucketに動画を投稿しています。署名されたPUT urlはそのトリックを行います。しかし、ファイルサイズが10MBを超えると動作しませんので、私はこれを行うことができるオープンソースを見つけましたが、オブジェクトのようなファイルを使用します。Pythonでstrデータをファイルオブジェクトに変換する
def read_in_chunks(file_object, chunk_size=65536):
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def main(file, url):
content_name = str(file)
content_path = os.path.abspath(file)
content_size = os.stat(content_path).st_size
print content_name, content_path, content_size
f = open(content_path)
index = 0
offset = 0
headers = {}
for chunk in read_in_chunks(f):
offset = index + len(chunk)
headers['Content-Type'] = 'application/octet-stream'
headers['Content-length'] = content_size
headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset, content_size)
index = offset
try:
r = requests.put(url, data=chunk, headers=headers)
print "r: %s, Content-Range: %s" % (r, headers['Content-Range'])
except Exception, e:
print e
私がビデオをアップロードする方法は、json形式のデータを渡すことでした。
class GetData(webapp2.RequestHandler):
def post(self):
data = self.request.get('file')
私がしたのは、すべてrequest.put(url、data = data)でした。これはシームレスに機能しました。
このデータを変換するには、Pythonはstrとしてオブジェクトのようなファイルに認識されますか?
ありがとう、これは解決策でした。今私は400レスポンスを得た理由を理解しなければなりません。しかし、ありがとう、これは私が探していたものです。 –