Skip to content

Commit

Permalink
Added send_me_file feature
Browse files Browse the repository at this point in the history
  • Loading branch information
marazmiki committed May 5, 2020
1 parent ceb7358 commit c843282
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
19 changes: 18 additions & 1 deletion django_selectel_storage/selectel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import io
from datetime import datetime, timedelta
from logging import getLogger as get_logger

import requests
from django.utils.module_loading import import_string

from .compat import TEXT_TYPE

log = get_logger('selectel')
now = datetime.now

Expand Down Expand Up @@ -79,7 +82,8 @@ def perform_request(self, http_method, key,
if resp.status_code == 401:
log.debug('Got an unexpected 401 error, reauthenticate.')
self.authenticate()
return self.perform_request(http_method, key, raise_exception, **kwargs)
return self.perform_request(http_method, key, raise_exception,
**kwargs)
if raise_exception:
resp.raise_for_status()
return resp
Expand Down Expand Up @@ -138,3 +142,16 @@ def list(self, key):
'prefix': key
}).json()
}

def send_me_file(self, filename, size):
headers = {
'Content-Type': 'x-storage/sendmefile+inplace',
'X-Object-Meta-Sendmefile-Max-Size': TEXT_TYPE(size),
'X-Object-Meta-Sendmefile-Disable-Web': 'yes',
'X-Object-Meta-Sendmefile-Allow-Overwrite': 'no',
'X-Object-Meta-Sendmefile-Ignore-Filename': 'yes',
'X-Filename': '/' + TEXT_TYPE(filename),
}
self.perform_request('put', filename, data=io.BytesIO(),
headers=headers, raise_exception=True)
return self.build_url(filename)
10 changes: 8 additions & 2 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ Changelog
1.x
---

1.0.2
-----

* A new feature ``sendmefile`` implementation to make direct upload to a container.
* Fixed an issue when authentication gets disappeared in some cases (#10, thanks to `@idealatom <https://github.com/idealatom>`_ for PR);
* Fixed some misspells in docs;

1.0.1
-----

* Fixed an issue when accessing a container without explicit authentication (thanks to `Alexey Kotenko <Alexey Kotenko
>`_ for reporting)
* Fixed an issue when accessing a container without explicit authentication (thanks to `Alexey Kotenko <https://github.com/k0t3n>`_ for reporting)
* Added ``requests`` as a required dependency.

1.0
Expand Down
30 changes: 25 additions & 5 deletions tests/test_selectel.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest

from django_selectel_storage import selectel
import io
import uuid

import pytest
import requests

def test_auth_if_empty_storage_url():
pass
from django_selectel_storage import selectel, utils


@pytest.mark.parametrize(
Expand Down Expand Up @@ -80,3 +80,23 @@ def patched_authenticate(self):
assert (
auth.build_url('index.html') == 'https://example.com/bucket/index.html'
)


def test_send_me_file():
container = selectel.Container(
config=utils.read_config([], {})
)
identifier = str(uuid.uuid4())

filename = 'send_me_file-' + identifier + '.txt'
contents = b'This is content of ' + filename.encode()

public_upload_link = container.send_me_file(filename, len(contents))

assert public_upload_link == container.build_url(filename)

resp = requests.put(public_upload_link, io.BytesIO(contents))
assert resp.status_code == 201

resp = requests.get(public_upload_link)
assert resp.content == contents

0 comments on commit c843282

Please sign in to comment.