使用python请求上传文件

Wur*_*rer 6 python-requests box-api

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

到目前为止我运气不好.也许这里有人可以帮助我看看我实际上做错了什么.

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,
    },
)
Run Code Online (Sandbox Code Playgroud)

我的身份验证有效,因为我正在使用相同的数据创建一个文件夹.

响应看起来像这样:

{
    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'
}
Run Code Online (Sandbox Code Playgroud)

也许这里的某个人遇到了类似的问题.

Mik*_*ter 7

您需要传递2个Python词典,文件和数据.文件是{uniqFileName:openFileObj},数据是{uniqFileName:filename}.以下是我的盒子类的上传方法.并记得在数据中添加最终条目'folder_id': destination_id.

def uploadFiles(self, ufiles, folid):
    '''uploads 1 or more files in the ufiles list of tuples containing
    (src fullpath, dest name). folid is the id of the folder to
    upload to.'''

    furl = URL2 + 'files/data'
    data, files = {}, {}
    for i, v in enumerate(ufiles):
        ff = v[0]
        fn = v[1]
        #copy to new, renamed file in tmp folder if necessary
        #can't find a way to do this with the api
        if os.path.basename(ff) != fn:
            dest = os.path.join(TMP, fn)
            shutil.copy2(ff, dest)
            ff = dest

        f = open(ff, 'rb')
        k = 'filename' + str(i)
        data[k] = fn
        files[k] = f

    data['folder_id'] = folid

    res = self.session.post(furl, files=files, data=data)

    for k in files:
        files[k].close()


    return res.status_code
Run Code Online (Sandbox Code Playgroud)

这是一个示例调用:

destFol = '406600304'

ret = box.uploadFile((('c:/1temp/hc.zip', 'hz.zip'),), destFol)
Run Code Online (Sandbox Code Playgroud)

就像我说的,上面的函数是一个类的方法,其中一个实例attr包含一个请求会话.但你可以使用requests.post而不是self.session.post,它将工作相同.如果您在会话之外执行此操作,请记住使用您的apikey和令牌添加标头.

根据文档,您应该能够通过在数据字典中为其指定新名称来重命名该文件.但我无法完成这项工作,除非将src文件复制到具有所需名称的临时目录并上传.这有点像黑客,但它确实有效.

祝你好运,迈克


Wur*_*rer 1

当有人要求我实现时,我想我会把它放在这里,供任何试图实现类似目标的人使用。

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
Run Code Online (Sandbox Code Playgroud)

因此,本质上,我需要发送文件并检索新更新的文件的 ID,并向 box 发送另一个请求。就我个人而言,我也不喜欢它,但它对我有用,并且无法找到任何其他从一开始就进行正确命名的实现。

希望有人可以从这个片段中受益。