如何在Amazon S3上存储scrapy图像?

时间:2012-05-06 16:42:37

标签: python amazon-s3 scrapy

我现在已经使用Scrapy大约1周了,并且想要将图像存储到亚马逊S3中,并且他们提到它们支持将图像上传到亚马逊S3,但它没有记录。那么有谁知道如何在Scrapy中使用Amazon S3?

以下是media pipeline的Scrapy文档。

3 个答案:

答案 0 :(得分:10)

您需要3个设置:

AWS_ACCESS_KEY_ID = "xxxxxx"
AWS_SECRET_ACCESS_KEY = "xxxxxx"
IMAGES_STORE = "s3://bucketname/base-key-dir-if-any/"

就是这样,即。图像将使用http://readthedocs.org/docs/scrapy/en/latest/topics/images.html#file-system-storage中描述的相同目录结构存储,即:

s3://bucketname/base-key-dir-if-any/full/3afec3b4765f8f0a07b78f98c07b83f013567a0a.jpg

答案 1 :(得分:6)

自上次回答以来已经过了几年,有些事情发生了变化(2015年)。 Nick Verwymeren写了一篇博文,详细介绍了如何做到这一点的更新版本。他的博文在这里:https://www.nickv.codes/blog/scrapy-uploading-image-files-to-amazon-s3/

您在settings.py文件中

ITEM_PIPELINES = {
    'scrapy.contrib.pipeline.images.ImagesPipeline': 1
}

# This is going to be the amazon s3 bucket. 
# You need to use the below format so Scrapy 
# can parse it. !!Important don't forget to add 
# the trailing slash.
IMAGES_STORE = 's3://my-bucket-name/'

# The amount of days until we re-download the image
IMAGES_EXPIRES = 180     

# You can add as many of these as you want
IMAGES_THUMBS = {
    'small': (50, 50), 
    'big': (300, 300)
}

AWS_ACCESS_KEY_ID = 'your-access-key'
AWS_SECRET_ACCESS_KEY= 'your-secret-access-key'

为了安全起见,我建议在Amazon AWS界面中创建一个新用户,并为该用户提供对您的存储桶的读/写权限。

现在我们需要安装一些默认情况下不带Scrapy的软件包:

pip install pillow
pip intall botocore

Pillow处理图像处理,boto将提供连接到S3的库。

Scrapy使用项目中的image_urls键查找应下载的图片。这应该是图片网址列表。下载后,Scrapy会将图像位置的详细信息写入图像密钥。

不要忘记将这些添加到items.py文件中:

class MyItem(scrapy.Item):
    image_urls = scrapy.Field()
    images = scrapy.Field()

现在不要忘记在抓取过程中实际填充image_urls键。一旦您抓取您的网站,最终输出对于给定项目将看起来像这样:

'image_urls': [u'http://example.com/images/tshirt.jpg'],
'images': [{ 'checksum': '264d3bbdffd4ab3dcb8f234c51329da8',
         'path': 'full/069f409fd4cdb02248d726a625fecd8299e6055e.jpg',
         'url': 'http://example.com/images/tshirt.jpg'}],

现在请向您介绍亚马逊S3水桶并看一看。你的图像和缩略图都在那里!

再次,非常感谢Nick Verwymeren完全回答这个问题的博客文章!

答案 2 :(得分:2)

@ 2083 我遇到了同样的问题。没有错误,并且已经安装了boto。 你可以在这里找到答案:https://doc.scrapy.org/en/latest/topics/feed-exports.html#topics-feed-storage-s3 “Scrapy仅在Python 2上支持boto” 我认为你像我一样使用Python 3。然后,我安装了botocore。它有效!