@@ -10,6 +10,7 @@ module MartenS3
1010 @endpoint : String ? = nil ,
1111 @force_path_style : Bool = false ,
1212 @expires_in = 86_400 ,
13+ @use_public_url : Bool = false
1314 )
1415 @client = Awscr ::S3 ::Client .new(
1516 @region ,
@@ -72,19 +73,34 @@ module MartenS3
7273 end
7374
7475 def url (filepath : String ) : String
75- generate_presigned_url(filepath)
76+ filepath = URI .encode_path(filepath)
77+
78+ if @use_public_url
79+ public_url(filepath)
80+ else
81+ generate_presigned_url(filepath)
82+ end
7683 end
7784
7885 private def public_url (filepath )
79- File .join(@endpoint .not_nil!, @bucket , URI .encode_path(filepath))
86+ if @force_path_style
87+ uri = @client .endpoint.dup
88+ uri.path = " /#{ @bucket } /#{ filepath } "
89+ else
90+ uri = @client .endpoint.dup
91+ uri.host = " #{ @bucket } .#{ @client .endpoint.host} "
92+ uri.path = " /#{ filepath } "
93+ end
94+
95+ uri.to_s
8096 end
8197
8298 private def generate_presigned_url (filepath : String )
8399 options = Awscr ::S3 ::Presigned ::Url ::Options .new(
84100 aws_access_key: @access_key ,
85101 aws_secret_key: @secret_key ,
86- region: @region ,
87- endpoint: @endpoint ,
102+ region: @client . region,
103+ endpoint: @client . endpoint.to_s ,
88104 bucket: @bucket ,
89105 force_path_style: @force_path_style ,
90106 object: filepath,
0 commit comments