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

Mah*_*tah 12 python amazon-s3 scrapy

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

这是他们用于媒体管道的Scrapy文档.

dan*_*gra 10

你需要3个设置:

AWS_ACCESS_KEY_ID = "xxxxxx"
AWS_SECRET_ACCESS_KEY = "xxxxxx"
IMAGES_STORE = "s3://bucketname/base-key-dir-if-any/"
Run Code Online (Sandbox Code Playgroud)

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

s3://bucketname/base-key-dir-if-any/full/3afec3b4765f8f0a07b78f98c07b83f013567a0a.jpg
Run Code Online (Sandbox Code Playgroud)


Sam*_*xas 7

自上次回答以来已经过了几年,有些事情发生了变化(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'
Run Code Online (Sandbox Code Playgroud)

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

现在我们需要安装一些默认情况下没有使用Scrapy的软件包:

pip install pillow
pip intall botocore
Run Code Online (Sandbox Code Playgroud)

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

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

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

class MyItem(scrapy.Item):
    image_urls = scrapy.Field()
    images = scrapy.Field()
Run Code Online (Sandbox Code Playgroud)

现在不要忘记在爬行过程中实际填充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'}],
Run Code Online (Sandbox Code Playgroud)

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

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