Reputation: 2778
in S3 presigned url how I can encoded the string $filename
s3.bucket(ENV.fetch('S3_BUCKET_NAME')).presigned_post(
key: "uploads/#{Time.now.to_i}/${filename}",
allow_any: ['authenticity_token'],
acl:'public-read',
metadata: {
'original-filename' => '${filename}'
},
success_action_status: "201"
)
sometime the filename include some special char or spaces. I would like to avoid them in the key
Upvotes: 2
Views: 286
Reputation: 5563
To cast your filename to url-safe form you may use 2 options:
.parameterize
method. See
https://apidock.com/rails/String/parameterize If you are using plain Ruby:
filename.gsub(%r{\s}, '_').gsub(%r{[^a-zA-Z0-9-.]+}, '')
Sample:
'asf asfa 1-240((($@))[email protected]'.gsub(%r{\s}, '_').gsub(%r{[^a-zA-Z0-9-.]+}, '') => "asfasfa1-240.jpeg"
Both approaches should throw away any spaces and special characters.
Upvotes: 2