Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cubicweb
cubes
s3storage
Commits
f657582ffdf4
Commit
677e7b58
authored
Feb 23, 2021
by
Elouan Martinet
Browse files
[storages] Add the ability to set upload extra args
parent
8b4ec082c4b9
Pipeline
#36566
failed with stage
in 1 minute and 30 seconds
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
cubicweb_s3storage/storages.py
View file @
f657582f
...
@@ -98,7 +98,9 @@ class S3Storage(Storage):
...
@@ -98,7 +98,9 @@ class S3Storage(Storage):
buffer
=
Binary
(
binary
.
read
())
buffer
=
Binary
(
binary
.
read
())
binary
.
seek
(
0
)
binary
.
seek
(
0
)
suffixed_key
=
self
.
suffixed_key
(
key
)
suffixed_key
=
self
.
suffixed_key
(
key
)
self
.
s3cnx
.
upload_fileobj
(
buffer
,
self
.
bucket
,
suffixed_key
)
extra_args
=
self
.
get_upload_extra_args
(
entity
,
attr
,
key
)
self
.
s3cnx
.
upload_fileobj
(
buffer
,
self
.
bucket
,
suffixed_key
,
ExtraArgs
=
extra_args
)
buffer
.
close
()
buffer
.
close
()
# move to final key in post commit event
# move to final key in post commit event
...
@@ -108,6 +110,13 @@ class S3Storage(Storage):
...
@@ -108,6 +110,13 @@ class S3Storage(Storage):
entity
.
eid
,
attr
,
self
.
bucket
,
suffixed_key
)
entity
.
eid
,
attr
,
self
.
bucket
,
suffixed_key
)
return
binary
return
binary
def
get_upload_extra_args
(
self
,
_entity
,
_attr
,
_key
):
"""Additional options for boto3's upload_fileobj method.
Documentation for supported options can be found at:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html#the-extraargs-parameter
"""
return
{}
def
entity_updated
(
self
,
entity
,
attr
):
def
entity_updated
(
self
,
entity
,
attr
):
"""an entity using this storage for attr has been updatded"""
"""an entity using this storage for attr has been updatded"""
return
self
.
entity_added
(
entity
,
attr
)
return
self
.
entity_added
(
entity
,
attr
)
...
...
test/test_s3storage.py
View file @
f657582f
import
re
import
re
from
contextlib
import
contextmanager
from
contextlib
import
contextmanager
from
mock
import
patch
from
six
import
PY3
from
six
import
PY3
...
@@ -88,6 +89,21 @@ class S3StorageTC(testing.S3StorageTestMixin, CubicWebTC):
...
@@ -88,6 +89,21 @@ class S3StorageTC(testing.S3StorageTestMixin, CubicWebTC):
keys
=
[
x
.
key
for
x
in
self
.
s3_bucket
.
objects
.
all
()]
keys
=
[
x
.
key
for
x
in
self
.
s3_bucket
.
objects
.
all
()]
self
.
assertNotIn
(
key
,
keys
)
self
.
assertNotIn
(
key
,
keys
)
def
test_upload_content_type
(
self
):
mime_type
=
'x-custom/mime-type'
with
self
.
admin_access
.
client_cnx
()
as
cnx
,
\
patch
(
'cubicweb_s3storage.storages.S3Storage'
'.get_upload_extra_args'
,
return_value
=
{
'ContentType'
:
mime_type
}):
image
=
create_image
(
cnx
,
b
'some content'
)
cnx
.
commit
()
s3storage
=
self
.
repo
.
system_source
.
storage
(
'Image'
,
'data'
)
s3_key
=
s3storage
.
get_s3_key
(
image
,
'data'
)
head
=
s3storage
.
s3cnx
.
head_object
(
Bucket
=
self
.
s3_bucket
.
name
,
Key
=
s3_key
)
self
.
assertEqual
(
head
[
'ContentType'
],
mime_type
)
class
S3StorageMigrationTC
(
testing
.
S3StorageTestMixin
,
CubicWebTC
):
class
S3StorageMigrationTC
(
testing
.
S3StorageTestMixin
,
CubicWebTC
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment