Skip to content

Commit

Permalink
Merge branch 'release/v0.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
knktc committed Sep 15, 2018
2 parents 6c34b37 + b420a9b commit 580d182
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.1.2] - 2018-09-15

### Added

- head function to get object info
- implement exists
- implement size


## [0.1.1] - 2018-09-09

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ Open your browser to visit <http://localhost:8000> .
* Qinstor Python SDK(on github): <https://github.com/yunify/qingstor-sdk-python>
* Qinstor Python SDK docs: <https://docs.qingcloud.com/qingstor/sdk/python/qingstor_sdk.html>
* Qinstor Restful API: <https://docs.qingcloud.com/qingstor/api/>
* Django Custom Storage HOW-TO: <https://docs.djangoproject.com/en/2.1/howto/custom-file-storage/>
3 changes: 2 additions & 1 deletion demo_site/demo_site/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@

urlpatterns = [
path('', admin.site.urls),
path('download/<filename>/', views.download)
path('download/<filename>/', views.download),
path('head/<filename>/', views.check_existence),
]
13 changes: 12 additions & 1 deletion demo_site/netdisk/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@create:2018-09-01 18:22
"""

from django.http import HttpResponse, Http404
from django.http import HttpResponse, Http404, JsonResponse
from django.core.files.storage import DefaultStorage
from django_qingstor_storage.backends import Qinstor404Exception as FileDoesNotExistException

Expand All @@ -27,3 +27,14 @@ def download(request, **kwargs):

response = HttpResponse(f.read())
return response


def check_existence(request, **kwargs):
""" check file existence
"""
filename = kwargs.get('filename')
print(filename)
if STORAGE.exists(filename):
return HttpResponse()
else:
raise Http404
35 changes: 32 additions & 3 deletions django_qingstor_storage/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,41 @@ def _open(self, name, mode='rb'):
tmp_file.seek(0)
return QingstorFile(tmp_file, name, self)

def _head(self, name):
""" check object status, will return object size and other info
about head api: https://docs.qingcloud.com/qingstor/api/object/head.html
:param name: file name, use full path if file in directory
:return: object info
:rtype: dict
"""
resp = self.bucket.head_object(name)
obj_info = {'exists': True if resp.status_code == 200 else False}
obj_info.update(resp.headers)

return obj_info

def exists(self, name):
""" always return False right now...
""" check file existence
:param name: file name, use full path if file in directory
:return: exists or not
:rtype: bool
"""
# todo check file existence
return self._head(name).get('exists')

def size(self, name):
""" get file size, in bytes, will return 0 if file not exists
return False
:param name: file name, use full path if file in directory
:return: size in bytes
:rtype: int
"""
obj_info = self._head(name)
if not obj_info.get('exists'):
return 0
else:
return obj_info.get('Content-Length')

def url(self, name, expires=60 * 5):
""" not sure is there any way to get a signed url by the sdk,
Expand Down

0 comments on commit 580d182

Please sign in to comment.