Skip to content

Commit

Permalink
Added support for writing to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Dec 5, 2020
1 parent e280042 commit b218b81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions frictionless/plugins/aws.py
@@ -1,12 +1,10 @@
import io
import os
from urllib.parse import urlparse
from ..exception import FrictionlessException
from ..control import Control
from ..plugin import Plugin
from ..loader import Loader
from .. import helpers
from .. import errors


# Plugin
Expand Down Expand Up @@ -110,8 +108,12 @@ def read_byte_stream_create(self):
# Write

def write_byte_stream_save(self, byte_stream):
error = errors.SchemeError(note="Writing to S3 is not supported")
raise FrictionlessException(error)
boto3 = helpers.import_from_plugin("boto3", plugin="aws")
control = self.resource.control
parts = urlparse(self.resource.source, allow_fragments=False)
client = boto3.resource("s3", endpoint_url=control.endpoint_url)
object = client.Object(bucket_name=parts.netloc, key=parts.path[1:])
object.put(Body=byte_stream)


# Internal
Expand Down
15 changes: 15 additions & 0 deletions tests/plugins/test_aws.py
Expand Up @@ -102,6 +102,21 @@ def test_table_s3_problem_with_spaces_issue_501(bucket_name):
assert table.read_data() == [["1", "english"], ["2", "中国人"]]


@mock_s3
def test_table_s3_write(bucket_name):
client = boto3.resource("s3", region_name="us-east-1")
client.create_bucket(Bucket=bucket_name, ACL="public-read")

# Write
with Table("data/table.csv") as table:
table.write("s3://%s/table.csv" % bucket_name)

# Read
with Table("s3://%s/table.csv" % bucket_name) as table:
assert table.header == ["id", "name"]
assert table.read_data() == [["1", "english"], ["2", "中国人"]]


# Fixtures


Expand Down

0 comments on commit b218b81

Please sign in to comment.