Googleドライブに任意のファイルタイプをアップロードするためのフォームとGoogleAppEngineを使用したシンプルなサーバーを作成しました。フォームは特定のファイルタイプでは機能せず、代わりに次のエラーが発生します。
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v1/files?alt=json returned "Unsupported content with type: application/pdf">
PDFファイルはサポートされていませんか?
アップロードを行うappengineコードは、次のようになります。
def upload_to_drive(self, filestruct):
resource = {
'title': filestruct.filename,
'mimeType': filestruct.type,
}
resource = self.service.files().insert(
body=resource,
media_body=MediaInMemoryUpload(filestruct.value,
filestruct.type),
).execute()
def post(self):
creds = StorageByKeyName(Credentials, my_user_id, 'credentials').get()
self.service = CreateService('drive', 'v1', creds)
post_dict = self.request.POST
for key in post_dict.keys():
if isinstance(post_dict[key], FieldStorage):#might need to import from cgi
#upload to drive and return link
self.upload_to_drive(post_dict[key]) #TODO: there should be error handling here
MSOfficeのドキュメントや画像に使用できました。テキストファイルでも機能せず、次のエラーが発生します。
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v1/files?alt=json returned "Multipart content has too many non-media parts">
リソース辞書の「mimeType」値の設定を解除して、Googleドライブに自動的に設定させてみました。また、MediaInMemoryUploadコンストラクターでmimeタイプの値の設定を解除してみました。悲しいことに、両方とも機能しませんでした。