简体   繁体   English

Carrierwave + fog + S3 + Rails无法在数据库中保存完整的URL

[英]Carrierwave + fog + S3 +Rails Cannot save full URL in database

I am trying to save the full url of the image uploaded with carrierwave in my database, not just the identifier. 我试图在我的数据库中保存使用carrierwave上传的图像的完整URL,而不仅仅是标识符。 I have the image URL available before saving it, however upon save only the identifier appears in the database. 在保存之前,我有可用的图像URL,但是在保存时,只有标识符出现在数据库中。 I have the host and endpoint set in the Carrierwave.rb config, as well as the asset_host. 我在Carrierwave.rb配置中设置了主机和端点,以及asset_host。 See below: 见下文:

CarrierWave.configure do |config|
  config.fog_provider = 'fog/aws'                        # required
  config.fog_credentials = {
    provider:              'AWS',                        # required
    aws_access_key_id:     'xxx',                        # required
    aws_secret_access_key: 'yyy',                        # required
    region:                'eu-west-1',                  # optional, defaults to 'us-east-1'
    host:                  's3.example.com',             # optional, defaults to nil
    endpoint:              'https://s3.example.com:8080' # optional, defaults to nil
  }
  config.fog_directory  = 'name_of_directory'                          # required
  config.fog_public     = true                                        # optional, defaults to true
config.asset_host = "https://s3.example.com:8080/name_of_directory"
end

The asset_host does not seem to be doing much in this case. 在这种情况下,asset_host似乎没有做太多。 Is there another way to make sure the full url is being saved? 还有另一种方法可以确保保存完整的URL吗?

Thank you! 谢谢!

Storing all the full urls could lead to a lot of duplication (as the host would be the same for all of them). 存储所有完整的URL会导致大量重复(因为主机对于所有这些都是相同的)。 As such, the approach here is to just store the part that is different and build the URL from the known host and stored identifier. 因此,这里的方法是仅存储不同的部分,并从已知主机和存储的标识符构建URL。 So when you load the file you should be able to find the url readily using the carrierwave interface (and it should respect the asset_host if supplied). 因此,当您加载文件时,您应该能够使用carrierwave接口轻松找到该URL(如果提供,它应该尊重asset_host)。

Is there a particular reason you need it to store the full urls here? 是否有特殊原因需要它来存储完整的网址?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM