Skip to content

BUG/TST: Read from Public s3 Bucket Without Creds #34877

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

Merged
merged 15 commits into from
Jul 15, 2020
Merged
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
34 changes: 31 additions & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,37 @@ def get_filepath_or_buffer(
filepath_or_buffer = filepath_or_buffer.replace("s3n://", "s3://")
fsspec = import_optional_dependency("fsspec")

file_obj = fsspec.open(
filepath_or_buffer, mode=mode or "rb", **(storage_options or {})
).open()
# If botocore is installed we fallback to reading with anon=True
# to allow reads from public buckets
err_types_to_retry_with_anon: List[Any] = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alimcmaster1 what happens if we just catch PermissionError? Do you know which cases raise a NoCredentialsError or ClientError?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A ClientError was raised by test_read_without_creds_from_pub_bucket in this 3.7 build - https://travis-ci.org/github/pandas-dev/pandas/jobs/706462160

We were catching NoCredentialsError before the fspec change - https://github.com/pandas-dev/pandas/pull/34266/files#diff-a37b395bed03f0404dec864a4529c97dL34 - so I decided to catch to be on the safe side. (But can't see a case that raises)

Thoughts?

try:
import_optional_dependency("botocore")
from botocore.exceptions import ClientError, NoCredentialsError

err_types_to_retry_with_anon = [
ClientError,
NoCredentialsError,
PermissionError,
]
except ImportError:
pass

try:
file_obj = fsspec.open(
filepath_or_buffer, mode=mode or "rb", **(storage_options or {})
).open()
# GH 34626 Reads from Public Buckets without Credentials needs anon=True
except tuple(err_types_to_retry_with_anon):
if storage_options is None:
storage_options = {"anon": True}
else:
# don't mutate user input.
storage_options = dict(storage_options)
storage_options["anon"] = True
file_obj = fsspec.open(
filepath_or_buffer, mode=mode or "rb", **(storage_options or {})
).open()

return file_obj, encoding, compression, True

if isinstance(filepath_or_buffer, (str, bytes, mmap.mmap)):
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/io/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from io import BytesIO
import os

import pytest

import pandas.util._test_decorators as td

from pandas import read_csv
import pandas._testing as tm


def test_streaming_s3_objects():
Expand All @@ -15,3 +19,30 @@ def test_streaming_s3_objects():
for el in data:
body = StreamingBody(BytesIO(el), content_length=len(el))
read_csv(body)


@tm.network
@td.skip_if_no("s3fs")
def test_read_without_creds_from_pub_bucket():
# GH 34626
# Use Amazon Open Data Registry - https://registry.opendata.aws/gdelt
result = read_csv("s3://gdelt-open-data/events/1981.csv", nrows=3)
assert len(result) == 3


@tm.network
@td.skip_if_no("s3fs")
def test_read_with_creds_from_pub_bucke():
# Ensure we can read from a public bucket with credentials
# GH 34626
# Use Amazon Open Data Registry - https://registry.opendata.aws/gdelt

with tm.ensure_safe_environment_variables():
# temporary workaround as moto fails for botocore >= 1.11 otherwise,
# see https://github.com/spulec/moto/issues/1924 & 1952
os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key")
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret")
df = read_csv(
"s3://gdelt-open-data/events/1981.csv", nrows=5, sep="\t", header=None,
)
assert len(df) == 5