Skip to content

Commit cac441a

Browse files
committed
Allow public url
1 parent 6930c62 commit cac441a

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dependencies:
66
github: martenframework/marten
77
awscr-s3:
88
github: taylorfinnell/awscr-s3
9-
version: ~> 0.9.0
9+
branch: master # TODO: Remove when
1010

1111
development_dependencies:
1212
sqlite3:

src/marten_s3/store.cr

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)