upyun / python-sdk Goto Github PK
View Code? Open in Web Editor NEWUPYUN Python SDK
UPYUN Python SDK
the parent of class UpYun
is object
, so I think this line is useless:
Line 24 in b6a9c65
readme 文档中, 分块上传部分的参数
with open('unix.png', 'rb') as f:
res = up.put('/upyun-python-sdk/xinu.png', f, checksum=True, multipart=True, block_size=1024*1024, kwargs=kwargs)
有误
应该是
with open('unix.png', 'rb') as f:
res = up.put('/upyun-python-sdk/xinu.png', f, checksum=True, multipart=True, block_size=1024*1024, **kwargs)
如果项目利用python sdk来上传、下载文件,大多应该是在terminal运行的吧? 如果没有progress bar,上传个稍微大点的文件就体验就特别不好了~~
我在我的项目写了一个往upyun上传文件,支持终端实时显示上传进度、上传速度等等信息的code。 你如果感兴趣,可以考虑加上类似的功能o(∩_∩)o
https://github.com/wh1100717/PolySpider/blob/master/src/PolySpider/FileUploadUtils.py
签名内容时的日期必须是RFC 1123的格式
在__do_http_request
方法里:
dt = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
那么对于不同的Locale生成的字符串不相同:
一, 24 6 2013 06:25:37 GMT
于是请求签名失败了
`
up = upyun.UpYun('_', secret='_*', endpoint=upyun.ED_AUTO)
with open('contentImg.jpg', 'rb') as f:
res = up.put('/style/xinu.jpg', f, checksum=True, need_resume=True)
`
一直 报错 :
AttributeError: 'UpYun' object has no attribute 'up_rest'
直接用up.get(url)获取png图片,返回的不是content内容(bytes),而是text内容(str),但是无法从str编码回bytes,导致图片无法显示
很抱歉在您百忙之中打扰您。
我希望使用SDK提供的【远程签名服务】,但需要计算出的签名是用于FORM API 。几经使用计算出来的签名总是被【signature error】,对照源码检查了半天也查不出哪里出错了。猜测可能policy这步问题比较大,不知道能否把计算policy的方法接口也开放了,可以直接提交相关参数然后得到FORM API的签名,而不必像现在如果需要FORM API 的签名,得自己实现policy之后传入。
或者,能否在auth_server.py提供一下FORM API的实现,我照着大神的写法排查下问题。
也阅读了SDK中policy的计算,但修改下用在python3下怎么也不好使。
你们自己试试,会抛出一个:
Header must be of type str or bytes, not <class 'int'>
Windows, python 3.5.2, requests 2.11.0, upyun 2.3.5,像下面这样用upyun python sdk:
import upyun
up = upyun.UpYun(bucket=BUCKET, username=USERNAME, password=PASSWORD)
up.put('/upyun-python-sdk/ascii.txt', 'abcdefghijklmnopqrstuvwxyz\n')
会有异常
Traceback (most recent call last):
File "R:\up\venv\lib\site-packages\requests\utils.py", line 752, in check_header_validity
if not pat.match(value):
TypeError: expected string or bytes-like object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "R:\up\venv\lib\site-packages\upyun\modules\httpipe.py", line 59, in do_http_pipe
timeout=self.timeout, files=files)
File "R:\up\venv\lib\site-packages\requests\sessions.py", line 457, in request
prep = self.prepare_request(req)
File "R:\up\venv\lib\site-packages\requests\sessions.py", line 390, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "R:\up\venv\lib\site-packages\requests\models.py", line 295, in prepare
self.prepare_headers(headers)
File "R:\up\venv\lib\site-packages\requests\models.py", line 409, in prepare_headers
check_header_validity(header)
File "R:\up\venv\lib\site-packages\requests\utils.py", line 756, in check_header_validity
"not %s" % (value, type(value)))
requests.exceptions.InvalidHeader: Header value 27 must be of type str or bytes, not <class 'int'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "upyun_test.py", line 5, in <module>
up.put('/upyun-python-sdk/ascii.txt', 'abcdefghijklmnopqrstuvwxyz\n')
File "R:\up\venv\lib\site-packages\upyun\upyun.py", line 91, in put
headers, handler, params, secret)
File "R:\up\venv\lib\site-packages\upyun\rest.py", line 87, in put
h = self.__do_http_request('PUT', key, value, headers)
File "R:\up\venv\lib\site-packages\upyun\rest.py", line 169, in __do_http_request
value, headers, stream)
File "R:\up\venv\lib\site-packages\upyun\modules\httpipe.py", line 81, in do_http_pipe
raise UpYunClientException(e)
upyun.modules.exception.UpYunClientException: Header value 27 must be of type str or bytes, not <class 'int'>
应该是交给requests的Content-Length是int类型造成的……应该str一下吧
最近在使用django + 又拍云,但是直接网页上传文件的时候出现这个问题。先读取本地文件再上传就没问题。
代码大致是这样的,request.FILES["file"]是网页form上传来的。
up = Upload()
uploaded_file = request.FILES["file"]
#print os.fstat(uploaded_file.fileno()).st_size
file_path = up.upload_private_pic("/dealers/" + str(dealer.id) + "/images/", uploaded_file)
print file_path
Upload就是自己写的一个类,简单的集成了sdk中的上传文件的功能。但是sdk中有一句fileno的语句会导致错误,和我上面注释掉的那句基本一样。
截图在这里
http://photo.weibo.com/3560808645/albums/detail/album_id/3616760132684350#!/mode/1/page/1
发现bug一枚。当网络连接到需要认证的无线网络时,usage的返回值是网页内容,这显然是不合理的。
不知道2.2.0版本有没有注意到这个问题。
把upyun sdk 用git submodule 加到project中,类似于这样子的目录结构
vendor/upyun
upyun不是用package的方式,导致没有办法直接通过
from vendor.upyun.upyun import UpYun
php的sdk有生成policy签名的功能:https://github.com/upyun/php-sdk/tree/master/examples/client-upload
想知道这个python sdk是否有相同的功能,以方便flask的web应用使用?
本sdk中的form上传方法还是由python发起上传过程,我需要的是其中的签名和policy生成,将policy发给前端 让用户浏览器发起上传过程
upyun.modules.exception.UpYunServiceException: ('329cdd4c0b4a1061c01c55081d2a00bb', 400, 'Bad Request', '{"msg":"need bucket name","code":40000003,"id":"329cdd4c0b4a1061c01c55081d2a00bb"}')
按照教程进行初始化:
up = upyun.UpYun('bucket','username','password',None,timeout=30,endpoint=upyun.ED_AUTO)
up.put('/test.txt', 'sdasdafsdafsdfasdfasd\n')
一直报如下错误:
raise UpYunServiceException(request_id, status, msg, err)
UpYunServiceException: ('fd4de16bc44ef869098584ce40485760', 400, 'Bad Request', u'{"msg":"need bucket name","code":40000003,"id":"fd4de16bc44ef869098584ce40485760"}')
bucket名称以及传入,却一直报错。
采用 .modules.sign模块下的make_policy方法获得policy 并且生成签名,上传文件时服务器报错decode policy error. 发现是由于make_policy的结果没有使用decode('utf-8'),导致结果带一个b前缀,造成服务端解码错误。测试将make_policy方法改为
def make_policy(data):
policy = json.dumps(data);
return base64.b64encode(b(policy)).decode()
可以成功调用。
另外在FormUpload类中的upload方法,data字典里的key 'service' 应该改为 'bucket'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.