简体   繁体   中英

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. I have the image URL available before saving it, however upon save only the identifier appears in the database. I have the host and endpoint set in the Carrierwave.rb config, as well as the 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. Is there another way to make sure the full url is being saved?

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). 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. 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).

Is there a particular reason you need it to store the full urls here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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