Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable connection to S3 API for Swift, and other minor fixes #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion invenio_s3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

When creating a new location which will use the S3 API, the URI needs to start
with ``s3://``, for example
``invenio files location s3_default s3://my-bucket --default`` will
``invenio files location s3-default s3://my-bucket --default`` will
create a new location, set it as default location for your instance and use the
bucket ``my-bucket``. For more information about this command check
`Invenio-Files-Rest <https://invenio-files-rest.readthedocs.io/en/latest/>`_
Expand Down
1 change: 1 addition & 0 deletions invenio_s3/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def init_s3fs_info(self):
'S3_SIGNATURE_VERSION', 's3v4'
),
},
default_block_size=current_app.config.get('S3FS_BLOCK_SIZE', None),
)

s3_endpoint = current_app.config.get('S3_ENDPOINT_URL', None)
Expand Down
15 changes: 12 additions & 3 deletions invenio_s3/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def _get_fs(self, *args, **kwargs):

return (fs, self.fileurl)

def initialize(self, size=0):
def initialize(self, size=0, acl='private'):
"""Initialize file on storage and truncate to given size."""
fs, path = self._get_fs()

if fs.exists(path):
fp = fs.rm(path)
fp = fs.open(path, mode='wb')
fp = fs.open(path, mode='wb', acl=acl)

try:
to_write = size
Expand Down Expand Up @@ -68,16 +68,25 @@ def delete(self):
fs.rm(path)
return True

def open(self, mode='rb', acl='private'):
"""Open file.

The caller is responsible for closing the file.
"""
fs, path = self._get_fs()
return fs.open(path, mode=mode, acl=acl)

def update(self,
incoming_stream,
seek=0,
size=None,
chunk_size=None,
acl='private',
progress_callback=None):
"""Update a file in the file system."""
old_fp = self.open(mode='rb')
updated_fp = S3FSFileStorage(
self.fileurl, size=self._size).open(mode='wb')
self.fileurl, size=self._size).open(mode='wb', acl=acl)
try:
if seek >= 0:
to_write = seek
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def app_config(app_config):
'FILES_REST_STORAGE_FACTORY'] = 'invenio_s3.s3fs_storage_factory'
app_config['S3_ENDPOINT_URL'] = None
app_config['S3_ACCESS_KEY_ID'] = 'test'
app_config['S3_SECRECT_ACCESS_KEY'] = 'test'
app_config['S3_SECRET_ACCESS_KEY'] = 'test'
return app_config


Expand All @@ -44,7 +44,7 @@ def s3_bucket(appctx):
session = boto3.Session(
aws_access_key_id=current_app.config.get('S3_ACCESS_KEY_ID'),
aws_secret_access_key=current_app.config.get(
'S3_SECRECT_ACCESS_KEY'),
'S3_SECRET_ACCESS_KEY'),
)
s3 = session.resource('s3')
bucket = s3.create_bucket(Bucket='test_invenio_s3')
Expand Down