如何使用Openload.co API上传文件

时间:2017-04-25 19:16:25

标签: python rest api multipartform-data

我正在尝试使用Openload REST API upload a file调用'240p.mp4'。

由于上传端点需要文件的SHA-1哈希,我通过执行以下操作:

url = "https://api.openload.co/1/file/ul?login={login}&key={key}&sha1={sha1}".format(
    login='YOUR_LOGIN',
    key='YOUR_API_KEY',
    sha1=sha1_hash,
)

p = {
    'url': url,
    'headers': {
        'User-Agent': self.ua,
    }
}
r = self.r.get(url=p['url'], headers=p['headers'])
j = r.json()

upload_link = j['result']['url']

所以我要求上传链接:

requests

他们建议做一个 CURL 但我更像是curl -F file1=@/path/to/file.txt https://13abc37.example.com/ul/jAZUhVzeU78 家伙^^

p = {
    'url': upload_link,
    'headers': {
        'user-agent': self.ua,
        'Content-Type': 'multipart/form-data; boundary="xxx"',
    },
    'files': {
        'file1': open('/scripts/wordpress/240p.mp4', "rb"),
      # I've also tried this (and some others)
      # 'file1': ('240.mp4', open('/scripts/wordpress/240p.mp4', "rb"), 'video/mp4')
    }
}
r = self.r.post(url=p['url'], headers=p['headers'], files=p['files'])

所以我尝试用POST请求复制它:

r.content¬
{
    "status": 500,
    "msg": "failed to read: closed"
}

但它返回此错误响应:

boundary="xxx"

理论上,状态500错误是来自服务器的错误。但为什么我会收到这个错误?

N1:我已明确设置/scripts/wordpress/240p.mp4,因为如果我不这样做的话。响应返回它丢失。所以我设定了它。

N2:文件self.r的路径正确无误。权限也是。

N3:我知道远程上传功能,但我需要从二进制文件上传(来自本地我的意思)

N4:非常明显但requests.session()public function index() { $this->_dataOut['head'] = $this->load->view('head','', TRUE); $this->_dataOut['header'] = $this->load->view('layout/header','',TRUE); $this->_dataOut['body'] = $this->load->view('publicPages/welcome_message','',TRUE); $this->_dataOut['footer'] = $this->load->view('layout/footer','',TRUE); $this->_dataOut['foot'] = $this->load->view('foot','',TRUE); $this->load->view('mainPage', $this->_dataOut); } 的分配

2 个答案:

答案 0 :(得分:0)

我弄明白为什么它不起作用。 让requests处理content-type标题可以解决问题。 多部分字段中name变量的值无关紧要,您可以将其称为file1file_upload或其他任何内容。

这对我有用:

filepath = '/scripts/wordpress/240p.mp4'

sha1 = hashlib.sha1()

BLOCKSIZE = 65536
with open(filepath, 'rb') as afile:
    buf = afile.read(BLOCKSIZE)
    while len(buf) > 0:
        sha1.update(buf)
        buf = afile.read(BLOCKSIZE)

sha1_hash = sha1.hexdigest()

url = "https://api.openload.co/1/file/ul?login={login}&key={key}&sha1={sha1}".format(
    login='YOUR_LOGIN',
    key='YOUR_API_KEY',
    sha1=sha1_hash,
)

p = {
    'url': url,
    'headers': {
        'User-Agent': self.ua,
    }
}
r = self.r.get(url=p['url'], headers=p['headers'])
j = r.json()

upload_link = j['result']['url']

p = {
    'url': upload_link,
    'headers': {
        'user-agent': self.ua,
    },
    'files': {
        'file1': open(filepath, 'rb'),
    }
}
r = self.r.post(url=p['url'], headers=p['headers'], files=p['files'])

答案 1 :(得分:0)

尝试此代码.....以便使用openload api上传文件。

首先使用此网址获取文件夹ID

https://api.openload.co/1/file/listfolder?login=XXXXXXX&key=XXXX

获取文件夹ID后,将文件夹ID,登录名,密钥添加到下面的代码。就是这样。

<html>
		<form action="demo3.php"  method="post" enctype="multipart/form-data" >
			<input type="file" name="file">
			<input type="submit" name="submit" id="submit" value="upload">
		</form>

		<?php
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, 'https://api.openload.co/1/file/ul?login=XXXXXXXXX&key=XXXXXXX&folder=3994051');
			// Set so curl_exec returns the result instead of outputting it.
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			// Get the response and close the channel.
			$response = curl_exec($ch);
			$json=json_decode($response);
			$url=$json->result->url;

			curl_close($ch);
			if(isset($_FILES['file']['tmp_name']))
			{
				
				$ch1=curl_init();
				$cfile=new CURLFile($_FILES['file']['tmp_name'],$_FILES['file']['name']);
				$cfile->setPostFilename(basename($_FILES["file"]["name"]));
				$data=array("file"=>$cfile);
				curl_setopt($ch1, CURLOPT_URL, $url);
				curl_setopt($ch1, CURLOPT_POST, true);
				curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
				curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
				$response1=curl_exec($ch1);
				$json1=json_decode($response1);
				echo $json1->result->url;
				echo $response1;
				curl_close($ch1);
				#echo $json1;
			}
		?>
</html>