1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-06 11:00:46 +00:00

fix: added feed_options as a keyword argument to GCSFeedStorage. (#6628)

This commit is contained in:
anubhav 2025-01-23 21:36:45 +05:30 committed by GitHub
parent d4b152bbf6
commit c03fb2abb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 2 deletions

View File

@ -276,7 +276,14 @@ class S3FeedStorage(BlockingFeedStorage):
class GCSFeedStorage(BlockingFeedStorage):
def __init__(self, uri: str, project_id: str | None, acl: str | None):
def __init__(
self,
uri: str,
project_id: str | None,
acl: str | None,
*,
feed_options: dict[str, Any] | None = None,
):
self.project_id: str | None = project_id
self.acl: str | None = acl
u = urlparse(uri)
@ -284,12 +291,26 @@ class GCSFeedStorage(BlockingFeedStorage):
self.bucket_name: str = u.hostname
self.blob_name: str = u.path[1:] # remove first "/"
if feed_options and feed_options.get("overwrite", True) is False:
logger.warning(
"GCS does not support appending to files. To "
"suppress this warning, remove the overwrite "
"option from your FEEDS setting or set it to True."
)
@classmethod
def from_crawler(cls, crawler: Crawler, uri: str) -> Self:
def from_crawler(
cls,
crawler: Crawler,
uri: str,
*,
feed_options: dict[str, Any] | None = None,
) -> Self:
return cls(
uri,
crawler.settings["GCS_PROJECT_ID"],
crawler.settings["FEED_STORAGE_GCS_ACL"] or None,
feed_options=feed_options,
)
def _store_in_thread(self, file: IO[bytes]) -> None:

View File

@ -523,6 +523,21 @@ class GCSFeedStorageTest(unittest.TestCase):
bucket_mock.blob.assert_called_once_with("export.csv")
blob_mock.upload_from_file.assert_called_once_with(f, predefined_acl=acl)
def test_overwrite_default(self):
with LogCapture() as log:
GCSFeedStorage("gs://mybucket/export.csv", "myproject-123", "custom-acl")
self.assertNotIn("GCS does not support appending to files", str(log))
def test_overwrite_false(self):
with LogCapture() as log:
GCSFeedStorage(
"gs://mybucket/export.csv",
"myproject-123",
"custom-acl",
feed_options={"overwrite": False},
)
self.assertIn("GCS does not support appending to files", str(log))
class StdoutFeedStorageTest(unittest.TestCase):
@defer.inlineCallbacks