xiewenya / django-aliyun-oss2-storage Goto Github PK
View Code? Open in Web Editor NEWdjango storage backend for aliyun oss2
License: MIT License
django storage backend for aliyun oss2
License: MIT License
上传文件要指定 object meta信息,下载文件时才能指定文件名。所以想找找是否有位置配置该信息
这段代码会错误地赋值导致无法在OSS上找到想要的对象。
self._name = name[len(self._storage.location):]
由于我使用了wagtail框架,上传图片时name
会是'original_image/01.jpg'
,而self._storage.location
我的设置是'/res/upload/'
,这段代码self._name
就被赋值为诡异的‘'mage/01.jpg'',这样在OSS上找不到对应的对象。
实际上self._name
应该赋值为'res/upload/original_image/01.jpg'
所以这里应该写为:
self._name = self._storage.location + name
self._name = self._name.lstrip('/')
麻烦看下对不对
你好,我在本地测试没有问题,部署到阿里云上出现504,我怀疑是nginx找不到media了,但是如何把media路径修改成oss呢,这是我现在的配置:
`# the upstream component nginx needs to connect to
upstream django {
server unix://opt/uwsgitest/NewBlog/blog.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
# server oss-cn-shanghai.aliyuncs.com;
}
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name pandacoder.top; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /Blog_media {
proxy_pass http://pandacoderblog.oss-cn-shanghai.aliyuncs.com;
#alias /opt/uwsgitest/media_root; # your Django project's media files - amend as required
}
location /static {
alias /opt/uwsgitest/NewBlog/static_root; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /opt/uwsgitest/NewBlog/uwsgi_params; # the uwsgi_params file you installed
}
}
`
这个包是不是不能处理最新的sts访问策略?
rt
尊敬的开发者您好,请合一下Pull requests 26 #26 以解决Windows下安装错误问题。
class AliyunFile(File):
def __init__(self, name, storage, mode):
self._storage = storage
self._name = name[len(self._storage.location)+1:]. # 这里+1,会造成返回的文件名切掉了第一个字母
self._mode = mode
self.file = six.BytesIO()
self._is_dirty = False
self._is_read = False
super(AliyunFile, self).__init__(self.file, self.name) # 这里应该是self._name
cannot import name 'force_text' from 'django.utils.encoding'
好像4.0后,已经将force_text函数移除了
最新的包没有包含 这个bug fix: 850b591
{% static 'app/bootstrap/css/bootstrap.min.css'%} 被解析成http://xxxxxxx.oss-cn-hangzhou.aliyuncs.com/static%2Fapp%2Fbootstrap%2Fcss%2Fbootstrap.min.css
如果bootstrap.min.css包含相对路径的图片,那么就会出现问题,请问这个情况如何解决?
期望解析成http://xxxxxxx.oss-cn-hangzhou.aliyuncs.com/static/app/bootstrap/css/bootstrap.min.css
cname配置了CDN的域名,然后我们现在全站都是HTTPS了,CDN也是。但是目前这个库返回的URL都是http开头的。
Amazon的S3的话是有一个AWS_S3_SECURE_URLS的配置。
但是如果能够动态的根据django的SECURE_PROXY_SSL_HEADER来决定是HTTPS还是HTTP的话,那就最佳了。谢谢!
https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER
noah@noah-ubuntu-desktop:/mnt/data/nwblog$ python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
File "manage.py", line 22, in
execute_from_command_line(sys.argv)
File "/home/noah/.local/lib/python2.7/site-packages/django/core/management/init.py", line 363, in execute_from_command_line
utility.execute()
File "/home/noah/.local/lib/python2.7/site-packages/django/core/management/init.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/noah/.local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/noah/.local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/noah/.local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
collected = self.collect()
File "/home/noah/.local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect
handler(path, prefixed_path, storage)
File "/home/noah/.local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 354, in copy_file
if not self.delete_file(path, prefixed_path, source_storage):
File "/home/noah/.local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 260, in delete_file
if self.storage.exists(prefixed_path):
File "/home/noah/.local/lib/python2.7/site-packages/django/utils/functional.py", line 238, in inner
self._setup()
File "/home/noah/.local/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 504, in _setup
self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
File "/home/noah/.local/lib/python2.7/site-packages/django/core/files/storage.py", line 467, in get_storage_class
return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
File "/home/noah/.local/lib/python2.7/site-packages/django/utils/module_loading.py", line 20, in import_string
module = import_module(module_path)
File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
ImportError: No module named aliyun-oss2-storage.backends
AttributeError: 'AliyunFile' object has no attribute 'name'出现这个问题不知道为什么
class AliyunFile(File):
def init(self, name, storage, mode):
self._storage = storage
self._name = name[len(self._storage.location)+1:]
self._mode = mode
self.file = six.BytesIO()
self._is_dirty = False
self._is_read = False
super(AliyunFile, self).init(self.file, self.name) 这里应该改为self._name
ooking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting django-aliyun-oss2-storage
Downloading https://mirrors.aliyun.com/pypi/packages/cd/59/e22479bac1c97ba9af8b7ce6e3723cd4eea420a8715aa58ac49d631b9604/django-aliyun-oss2-storage-0.1.5.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\newpepsi\AppData\Local\Temp\pycharm-packaging\django-aliyun-oss2-storage\setup.py", line 5, in
README = readme.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x91 in position 63: illegal multibyte sequence
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\newpepsi\AppData\Local\Temp\pycharm-packaging\django-aliyun-oss2-storage\
DEFAULT_FILE_STORAGE = 'aliyun_oss2_storage.backends.AliyunMediaStorage'
STATICFILES_STORAGE = 'aliyun_oss2_storage.backends.AliyunStaticStorage'
你好,这两个配置我测试出来的结果是,配置DEFAULT_FILE_STORAGE功能正常,如果再配置STATICFILES_STORAGE则页面加载资源的所有连接会添加END_POINT前缀,但是比如说配合xadmin这些框架使用的时候,资源不需要加载end_point。
另外还有一个建议,能否保存图片的时候重新命名保存??
我具体对这两个配置的作用还没有看源码,python只懂基础语法,django大致了解models一些基础,希望能做一些精准的文档。
我檔案名稱是
/2016-07-21/小米5钢化膜_sales_False_mobile_ranking/item
出來的url是
http://...oss-cn-shanghai.aliyuncs.com/2016-07-21%2F%25E5%25B0%258F%25E7%25B1%25B35%25E9%2592%25A2%25E5%258C%2596%25E8%2586%259C_sales_False_mobile_ranking%2Fitems
檔案路徑被 urlencode 2 次
File "/Users/ty/work/virtualenvs/hg_backend3/lib/python3.5/site-packages/aliyun_oss2_storage/backends.py", line 7, in
from urlparse import urljoin
ImportError: No module named 'urlparse'
环境: python3.5 django Django (1.9.6)
content = self._storage.bucket.get_object(self._name)
self.file = six.BytesIO(content)
这里的content
是oss2.models.GetObjectResult
对象,直接作为参数传给six.BytesIO()
会返回类似于'<oss2.models.GetObjectResult object at 0x7f59ddc0f250>'
这样的字符串,导致无法读取图片。
传给six.BytesIO()
的应该为content.read()
path里面的斜杠都变成%2F
本地测试的时候.
'details': "RequestError: HTTPConnectionPool(host='xx.oss-cn-hangzhou.aliyuncs.com', port=80): Read timed out. (read timeout=60)", 'status': -2
博主你好,我直接在views里面定义相关的access_id & access_key进行验证oss,控制台输出可以返回一个oss的object,但是想去遍历这个的时候出现报错,如下:
System check identified no issues (0 silenced).
August 30, 2018 - 06:01:08
Django version 1.11, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
<oss2.api.Bucket object at 0x7f1c675b7d90>
Internal Server Error: /mark_index/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/lord/imooc/mysite/marking/views.py", line 26, in index
for obj in oss2.ObjectIterator(bucket, delimiter='/'):
File "/usr/local/lib/python2.7/dist-packages/oss2/iterators.py", line 43, in next
return self.next()
File "/usr/local/lib/python2.7/dist-packages/oss2/iterators.py", line 40, in next
self.fetch_with_retry()
File "/usr/local/lib/python2.7/dist-packages/oss2/iterators.py", line 48, in fetch_with_retry
self.is_truncated, self.next_marker = self._fetch()
File "/usr/local/lib/python2.7/dist-packages/oss2/iterators.py", line 108, in _fetch
max_keys=self.max_keys)
File "/usr/local/lib/python2.7/dist-packages/oss2/api.py", line 331, in list_objects
'encoding-type': 'url'})
File "/usr/local/lib/python2.7/dist-packages/oss2/api.py", line 1128, in __do_object
return self._do(method, self.bucket_name, key, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/oss2/api.py", line 154, in _do
raise exceptions.make_exception(resp)
AccessDenied: {'status': 403, 'request-id': '5B87882C95F081E9CA6611B4', 'details': {'HostId': 'uncertain.oss-cn-beijing.aliyuncs.com', 'Message': 'OSS authentication requires a valid Date.', 'Code': 'AccessDenied', 'RequestId': '5B87882C95F081E9CA6611B4'}}
ERROR:django.request:Internal Server Error: /mark_index/
view中代码如下:
def index(request):
# 遍历oss云端bucket
auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET)
bucket = oss2.Bucket(auth, END_POINT, 'uncertain')
print bucket
for obj in oss2.ObjectIterator(bucket, delimiter='/'):
print('Processing')
welcom = u"这是首页!!"
return render(request, 'marking/index.html', {'welcom': welcom})
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.