Method: CarrierWave::Storage::Fog::File#public_url

Defined in:
lib/carrierwave/storage/fog.rb

#public_urlObject

Return a url to a public file, if available

Returns

String

public url

or
NilClass

no public url available



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/carrierwave/storage/fog.rb', line 365

def public_url
  encoded_path = encode_path(path)
  if (host = @uploader.asset_host)
    if host.respond_to? :call
      "#{host.call(self)}/#{encoded_path}"
    else
      "#{host}/#{encoded_path}"
    end
  else
    # AWS/Google optimized for speed over correctness
    case fog_provider
    when 'AWS'
      # check if some endpoint is set in fog_credentials
      if @uploader.fog_credentials.has_key?(:endpoint)
        raise 'fog_aws_fips = true is incompatible with :endpoint, as FIPS endpoints do not support path-style URLs.' if @uploader.fog_aws_fips
        "#{@uploader.fog_credentials[:endpoint]}/#{@uploader.fog_directory}/#{encoded_path}"
      else
        protocol = @uploader.fog_use_ssl_for_aws ? "https" : "http"

        subdomain_regex = /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/
        # To use the virtual-hosted style, the bucket name needs to be representable as a subdomain
        use_virtual_hosted_style = @uploader.fog_directory.to_s =~ subdomain_regex && !(protocol == 'https' && @uploader.fog_directory =~ /\./)

        region = @uploader.fog_credentials[:region].to_s
        regional_host = 's3.amazonaws.com' # used for DEFAULT_S3_REGION or no region set
        if @uploader.fog_aws_fips
          regional_host = "s3-fips.#{region}.amazonaws.com" # https://aws.amazon.com/compliance/fips/
        elsif ![DEFAULT_S3_REGION, ''].include?(region)
          regional_host = "s3.#{region}.amazonaws.com"
        end

        if use_virtual_hosted_style
          regional_host = 's3-accelerate.amazonaws.com' if @uploader.fog_aws_accelerate
          "#{protocol}://#{@uploader.fog_directory}.#{regional_host}/#{encoded_path}"
        else # directory is not a valid subdomain, so use path style for access
          raise 'FIPS Endpoints can only be used with Virtual Hosted-Style addressing.' if @uploader.fog_aws_fips
          "#{protocol}://#{regional_host}/#{@uploader.fog_directory}/#{encoded_path}"
        end
      end
    when 'Google'
      # https://cloud.google.com/storage/docs/access-public-data
      "https://storage.googleapis.com/#{@uploader.fog_directory}/#{encoded_path}"
    else
      # avoid a get by just using local reference
      directory.files.new(:key => path).public_url
    end
  end
end