首页 > 解决方案 > Rails 5 - 检索 MiniMagick 版本的图像(CarrierWave/Google Cloud Storage)

问题描述

我目前正在使用 Google Cloud Storage 和 CarrierWave 来处理我的文件上传。

为了能够清楚地解释“问题”,假设我有一个名为的模型,该模型具有保存图像字符串User的列。avatar

用户上传文件后,完成:

version :foo1 do
  process :auto_orient
  process resize_to_fill: [150, 150]
end

version :foo2 do
  process :auto_orient
  process resize_to_fill: [250, 250]
end

version :foo3 do
  process :auto_orient
  process resize_to_fill: [350, 350]
end

它成功地在我的存储桶中创建了各种版本的图像。

所以问题出在查看我的日志时,我意识到当我这样做时

<%= image_tag(User.avatar.foo1.url) %>

查询实际上是针对图像的每一个变体执行的,因此它会foo1-3在渲染之前调用原始变体以及foo1

所以问题是,我如何检索只需要的特定变化?(我不确定我的上传器或载波代码是否在这里必不可少。如果是,请告诉我,我会将其包含在内)

作为参考,这是我在执行时在日志中看到的内容User.avatar.foo1.url,这让我认为我正在对 Google Cloud Storage 进行“过度”调用。(我用 foo 替换了我认为可能敏感的某些细节)

Sending HTTP get https://www.googleapis.com/storage/v1/b/foo?
200
#<Hurley::Response GET https://www.googleapis.com/storage/v1/b/foo == 200 (374 bytes) 69ms>
Success - #<Google::Apis::StorageV1::Bucket:foo
 @etag="CAI=",
 @id="foo",
 @kind="storage#bucket",
 @location="ASIA-SOUTHEAST1",
 @metageneration="2",
 @name="foo",
 @project_number="foo",
 @self_link="https://www.googleapis.com/storage/v1/b/foo",
 @storage_class="NEARLINE",
 @time_created=Wed, 20 Dec 2017 05:50:41 +0000,
 @updated=Wed, 20 Dec 2017 07:08:46 +0000>

Sending HTTP get https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2FWeChat_Image_20180112102430.jpg?
200
#<Hurley::Response GET https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2FWeChat_Image_20180112102430.jpg == 200 (926 bytes) 62ms>
Success - #<Google::Apis::StorageV1::Object:0x007f98b3332220
 @bucket="foo",
 @content_type="image/jpeg",
 @crc32c="5SNA/w==",
 @etag="CNrr8om4h9sCEAE=",
 @generation="1526377710663130",
 @id=
  "foo/uploads/profile/avatar/328/WeChat_Image_20180112102430.jpg/1526377710663130",
 @kind="storage#object",
 @md5_hash="g6cUQBBiGC31uRJUtM1dng==",
 @media_link=
  "https://www.googleapis.com/download/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2FWeChat_Image_20180112102430.jpg?generation=1526377710663130&alt=media",
 @metageneration="1",
 @name="uploads/profile/avatar/328/WeChat_Image_20180112102430.jpg",
 @self_link=
  "https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2FWeChat_Image_20180112102430.jpg",
 @size="5499",
 @storage_class="NEARLINE",
 @time_created=Tue, 15 May 2018 09:48:30 +0000,
 @time_storage_class_updated=Tue, 15 May 2018 09:48:30 +0000,
 @updated=Tue, 15 May 2018 09:48:30 +0000>

Sending HTTP get https://www.googleapis.com/storage/v1/b/foo?
200
#<Hurley::Response GET https://www.googleapis.com/storage/v1/b/foo == 200 (374 bytes) 53ms>
Success - #<Google::Apis::StorageV1::Bucket:foo
 @etag="CAI=",
 @id="foo",
 @kind="storage#bucket",
 @location="ASIA-SOUTHEAST1",
 @metageneration="2",
 @name="foo",
 @project_number="foo",
 @self_link="https://www.googleapis.com/storage/v1/b/foo",
 @storage_class="NEARLINE",
 @time_created=Wed, 20 Dec 2017 05:50:41 +0000,
 @updated=Wed, 20 Dec 2017 07:08:46 +0000>

Sending HTTP get https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2Fthumb_WeChat_Image_20180112102430.jpg?
200
#<Hurley::Response GET https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2Fthumb_WeChat_Image_20180112102430.jpg == 200 (950 bytes) 58ms>
Success - #<Google::Apis::StorageV1::Object:foo
 @bucket="foo",
 @content_type="image/jpeg",
 @crc32c="XDUTLQ==",
 @etag="CN/7+oq4h9sCEAE=",
 @generation="1526377712893407",
 @id=
  "foo/uploads/profile/avatar/328/thumb_WeChat_Image_20180112102430.jpg/1526377712893407",
 @kind="storage#object",
 @md5_hash="CRdX68ncBsEqJFY/uRg03A==",
 @media_link=
  "https://www.googleapis.com/download/storage/v1/b/deanslist-9322/o/uploads%2Fprofile%2Favatar%2F328%2Fthumb_WeChat_Image_20180112102430.jpg?generation=1526377712893407&alt=media",
 @metageneration="1",
 @name="uploads/profile/avatar/328/thumb_WeChat_Image_20180112102430.jpg",
 @self_link=
  "https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2Fthumb_WeChat_Image_20180112102430.jpg",
 @size="5923",
 @storage_class="NEARLINE",
 @time_created=Tue, 15 May 2018 09:48:32 +0000,
 @time_storage_class_updated=Tue, 15 May 2018 09:48:32 +0000,
 @updated=Tue, 15 May 2018 09:48:32 +0000>

Sending HTTP get https://www.googleapis.com/storage/v1/b/foo?
200
#<Hurley::Response GET https://www.googleapis.com/storage/v1/b/foo == 200 (374 bytes) 59ms>
Success - #<Google::Apis::StorageV1::Bucket:0x007f98b3252328
 @etag="CAI=",
 @id="foo",
 @kind="storage#bucket",
 @location="ASIA-SOUTHEAST1",
 @metageneration="2",
 @name="foo",
 @project_number="1028403006209",
 @self_link="https://www.googleapis.com/storage/v1/b/foo",
 @storage_class="NEARLINE",
 @time_created=Wed, 20 Dec 2017 05:50:41 +0000,
 @updated=Wed, 20 Dec 2017 07:08:46 +0000>

Sending HTTP get https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2Ftestimonial_WeChat_Image_20180112102430.jpg?
200
#<Hurley::Response GET https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2Ftestimonial_WeChat_Image_20180112102430.jpg == 200 (974 bytes) 59ms>
Success - #<Google::Apis::StorageV1::Object:0x007f98b6a82a40
 @bucket="foo",
 @content_type="image/jpeg",
 @crc32c="VFIp7Q==",
 @etag="CJbO2Yu4h9sCEAE=",
 @generation="1526377714444054",
 @id=
  "foo/uploads/profile/avatar/328/testimonial_WeChat_Image_20180112102430.jpg/1526377714444054",
 @kind="storage#object",
 @md5_hash="QGE8XArsE5C9QD2A6ybpVg==",
 @media_link=
  "https://www.googleapis.com/download/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2Ftestimonial_WeChat_Image_20180112102430.jpg?generation=1526377714444054&alt=media",
 @metageneration="1",
 @name=
  "uploads/profile/avatar/328/testimonial_WeChat_Image_20180112102430.jpg",
 @self_link=
  "https://www.googleapis.com/storage/v1/b/foo/o/uploads%2Fprofile%2Favatar%2F328%2Ftestimonial_WeChat_Image_20180112102430.jpg",
 @size="2481",
 @storage_class="NEARLINE",
 @time_created=Tue, 15 May 2018 09:48:34 +0000,
 @time_storage_class_updated=Tue, 15 May 2018 09:48:34 +0000,
 @updated=Tue, 15 May 2018 09:48:34 +0000>

标签: ruby-on-railsgoogle-cloud-storagecarrierwave

解决方案


推荐阅读