Skip to content

Commit

Permalink
Added methods: create_resource_key, create_resource_uri, and create_r…
Browse files Browse the repository at this point in the history
…esource to S3Bucket
  • Loading branch information
eterna2 committed Nov 12, 2019
1 parent fa45224 commit 8c33c7d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -18,12 +18,13 @@ API documentation can be found at [https://e2fyi-utils.readthedocs.io/en/latest/
#### CHANGELOG

| version | description |
| -------------- | ---------------------------------------------------------------------------------- |
| v0.1.0a1 | Initial release. Support AWS S3 bucket. |
| v0.1.0a1.post1 | Add README to pypi. |
| v0.1.0a2 | Fix bug: include requirements.txt into setup.py |
| v0.1.0a3 | Fix bug: incude e2fyi.utils.aws, e2fyi.utils.core and e2fyi.utils.io into setup.py |
| version | description |
| -------------- | ---------------------------------------------------------------------------------------- |
| v0.1.0a1 | Initial release. Support AWS S3 bucket. |
| v0.1.0a1.post1 | Add README to pypi. |
| v0.1.0a2 | Fix bug: include requirements.txt into setup.py |
| v0.1.0a3 | Fix bug: incude e2fyi.utils.aws, e2fyi.utils.core and e2fyi.utils.io into setup.py |
| v0.1.0a4 | Added methods: create_resource_key, create_resource_uri, and create_resource to S3Bucket |

## Quickstart

Expand Down
75 changes: 75 additions & 0 deletions e2fyi/utils/aws/s3.py
Expand Up @@ -442,6 +442,47 @@ def __init__(
def __str__(self) -> str:
return self.name

def create_resource_key(self, filename: str) -> str:
"""
Creates a resource key based on the s3 bucket name, and configured prefix.
Example::
from e2fyi.utils.aws import S3Bucket
s3 = S3Bucket(name="foo", get_prefix=lambda x: "bar/%s" % x)
print(s3.create_resource_key("hello.world")) # > bar/hello.world
Args:
filename (str): path for the file.
Returns:
str: key for the resource in s3.
"""
return self._get_prefix(filename)

def create_resource_uri(self, filename: str, protocol: str = "s3a://") -> str:
"""
Create a resource uri based on the s3 bucket name, and configured prefix.
Example::
from e2fyi.utils.aws import S3Bucket
s3 = S3Bucket(name="foo", get_prefix=lambda x: "bar/%s" % x)
print(s3.create_resource_uri("hello.world")) # > s3a://foo/bar/hello.world
Args:
filename (str): path for the file.
protocol (str, optional): protocol for the uri. Defaults to "s3a://".
Returns:
str: uri string for the resource.
"""
return "%s%s/%s" % (protocol, self.name, self.create_resource_key(filename))

def upload(
self,
filepath: str,
Expand Down Expand Up @@ -511,3 +552,37 @@ def list(
except botocore.exceptions.ClientError as exc:
return Result(None, exception=exc)
return Result(items)

def create_resource(
self,
filename: str,
content_type: str,
protocol: str = "s3a://",
stream: Union[io.StringIO, io.BytesIO, IO[StringOrBytes]] = None,
metadata: Dict[str, str] = None,
) -> S3Resource:
"""
create_s3_resource creates a new instance of S3Resource binds to the
current bucket.
Args:
filename (str): name of the resource.
content_type (str): mime type.
protocol (str, optional): protocol. Defaults to "s3a://".
stream (Union[io.StringIO, io.BytesIO, IO[StringOrBytes]], optional):
content of the resource. Defaults to None.
metadata (Dict[str, str], optional): metadata for the resource.
Defaults to None.
Returns:
S3Resource: a S3Resource related to the active S3Bucket.
"""
return S3Resource(
filename=filename,
content_type=content_type,
protocol=protocol,
bucketname=self.name,
prefix=self.prefix,
stream=stream,
metadata=metadata,
)
12 changes: 12 additions & 0 deletions e2fyi/utils/aws/s3_test.py
Expand Up @@ -239,3 +239,15 @@ def test_upload_pandasdf_asrecord(self):
{"col1": 4, "col2": "foo"},
],
)

def test_create_resource_key(self):
bucket = S3Bucket(name="bucket", get_prefix=lambda x: "folder/%s" % x)
key = bucket.create_resource_key("filename.ext")

self.assertEqual(key, "folder/filename.ext")

def test_create_resource_uri(self):
bucket = S3Bucket(name="bucket", get_prefix=lambda x: "folder/%s" % x)
key = bucket.create_resource_uri("filename.ext")

self.assertEqual(key, "s3a://bucket/folder/filename.ext")
2 changes: 1 addition & 1 deletion version.txt
@@ -1 +1 @@
0.1.0a3
0.1.0a4

0 comments on commit 8c33c7d

Please sign in to comment.