alioss.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. ## -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import oss2
  4. from django.conf import settings
  5. class AliOSS(object):
  6. def __init__(self,
  7. access_key_id = settings.ALI_OSS_ACCESS_ID,
  8. access_key_secret = settings.ALI_OSS_ACCESS_KEY,
  9. endpoint = settings.ALI_OSS_ENDPOINT,
  10. bucket_name = settings.ALI_OSS_BUKET_NAME):
  11. auth = oss2.Auth(access_key_id, access_key_secret)
  12. self.bucket = oss2.Bucket(auth, endpoint, bucket_name, enable_crc = False)
  13. def put_object(self, key, data, headers = {
  14. # 'Content-Encoding': 'br',
  15. 'Content-Disposition': 'inline',
  16. 'Cache-Control': 'max-age=315360000'
  17. }):
  18. self.bucket.put_object(key = key, data = data, headers = headers)
  19. def put_attachment(self, key, data, headers = {
  20. 'Content-Disposition': 'attachment',
  21. 'Cache-Control': 'max-age=315360000'
  22. }):
  23. self.bucket.put_object(key = key, data = data, headers = headers)
  24. def get_object(self, key, headers={
  25. 'Content-Disposition': 'attachment',
  26. 'Cache-Control': 'max-age=315360000'
  27. }):
  28. content = bytes()
  29. for chunk in self.bucket.get_object(key, headers=headers):
  30. content += chunk
  31. return content