使用python请求上传文件 [英] Upload file using python requests

查看:183
本文介绍了使用python请求上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用带有请求的框v2 api上传文件。

I've been trying to upload a file using the box v2 api with requests.

到目前为止,我运气不佳。也许这里有人可以帮助我看看我实际上在做错什么。

So far I had little luck though. Maybe someone here can help me see what I'm actually doing wrong.

file_name = "%s%s" % (slugify(sync_file.description), file_suffix)
file_handle = open(settings.MEDIA_ROOT + str(sync_file.document), 'rb')
folder_id = str(sync_file.patient.box_patient_folder_id)

r = requests.post(
    files_url,
    headers=headers,
    files={
        file_name: file_handle,
        "folder_id": folder_id,
    },
)

我的身份验证有效,因为我在之前创建了一个文件夹

My authentication works, because I'm creating a folder just before that, using the same data.

响应看起来像这样:

{
    u'status': 404, 
    u'code': u'not_found', 
    u'help_url': u'http://developers.box.com/docs/#errors', 
    u'request_id': u'77019510950608f791a0c1', 
    u'message': u'Not Found', 
    u'type': u'error'
}

也许有人在这里遇到了类似的问题。

Maybe someone on here ran into a similar issue.

推荐答案

当有人要求我实施该程序时,我想我会把它提交给任何试图实现这一目标的人

As someone requested my implementation, I figured I would put it out here for anyone trying to achieve something similar.

files_url = "%s/files/content" % (settings.BOX_API_HOST)
headers = {"Authorization": "BoxAuth api_key=%s&auth_token=%s" % 
              (settings.BOX_API_KEY, self.doctor.box_auth_token)
          }

file_root, file_suffix = os.path.splitext(str(self.document))
filename = "%s%s" % (slugify(self.description), file_suffix)
files = {
        'filename1': open(settings.MEDIA_ROOT + str(self.document), 'rb'),
        }
data = {
        'filename1': filename,
        'folder_id': str(self.patient.get_box_folder()),
       }

r = requests.post(files_url,
                  headers=headers,
                  files=files,
                  data=data)

file_response = simplejson.loads(r.text)

try:
    if int(file_response['entries'][0]['id']) > 0:
        box_file_id = int(file_response['entries'][0]['id'])

        #Update the name of file
        file_update_url = "%s/files/%s" % (settings.BOX_API_HOST, box_file_id) 
        data_update = {"name":  filename}
        file_update = requests.put(file_update_url,
                                   data=simplejson.dumps(data_update),
                                   headers=headers)

        LocalDocument.objects.filter(id=self.id).update(box_file_id=box_file_id)
except:
    pass

因此,从本质上讲,我需要发送文件并获取新更新文件的ID,然后将另一个请求发送至box 。就个人而言,我也不喜欢它,但是它对我有用,而且还无法从一开始就找到其他能够正确命名的实现。

So in essence, I needed to send the file and retrieve the ID of the newly updated file and send another request to box. Personally, I don't like it either, but it works for me and haven't been able to find any other implementations that do the correct naming from the get-go.

希望有人可以从此摘要中受益。

Hope someone can benefit from this snippet.

这篇关于使用python请求上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆