bunny_storage_client 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bunny_storage_client.rb +42 -6
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a47aba2b6b09ecdcc4d403f38277681fd74e025217f59d73b7586526c35adc97
|
4
|
+
data.tar.gz: 0d78f8d530772f3cfa651a9633b5eb51db0055c42069b16092cd84c7d80f970c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8dd4261af13d733df413a768131fc5ffe2b1bfa30a480641ddfef25fc0262609e57736837c802661bc6d2c55bc9a72613761a433eeffa0c404d0c28e4ea547b4
|
7
|
+
data.tar.gz: a0dcb474c93610736d98c99c478438e032cc6735546ce92d72a3b5f9ade46a691b46bbd7be899751eab97fef45e0d06284cc5b3d8f97452975710edec16bc905
|
data/lib/bunny_storage_client.rb
CHANGED
@@ -7,7 +7,7 @@ require 'logger'
|
|
7
7
|
# BunnyStorageClient is a Ruby SDK for interacting with BunnyCDN storage services.
|
8
8
|
# API Reference: https://docs.bunny.net/reference/storage-api
|
9
9
|
class BunnyStorageClient
|
10
|
-
VERSION = '1.0.
|
10
|
+
VERSION = '1.0.1'
|
11
11
|
BASE_URL = 'https://storage.bunnycdn.com/'
|
12
12
|
|
13
13
|
# Initializes the SDK with access credentials and optional logger.
|
@@ -16,11 +16,14 @@ class BunnyStorageClient
|
|
16
16
|
# @param api_key [String] the API key for cache purging
|
17
17
|
# @param storage_zone_name [String, nil] the default storage zone name
|
18
18
|
# @param logger [Logger] the logger instance for logging errors and info
|
19
|
-
def initialize(access_key, api_key, storage_zone_name = nil, logger = Logger.new($stdout))
|
19
|
+
def initialize(access_key, api_key, storage_zone_name = nil, region = nil, logger = Logger.new($stdout))
|
20
20
|
@access_key = access_key
|
21
21
|
@api_key = api_key
|
22
|
+
@region = region
|
22
23
|
@storage_zone_name = storage_zone_name
|
23
24
|
@logger = logger
|
25
|
+
|
26
|
+
@base_url = @region ? "https://#{@region}.storage.bunnycdn.com/" : BASE_URL
|
24
27
|
end
|
25
28
|
|
26
29
|
# Sets the object filename and optional storage zone name for operations.
|
@@ -49,6 +52,8 @@ class BunnyStorageClient
|
|
49
52
|
request['accept'] = '*/*'
|
50
53
|
response = make_request(uri, request)
|
51
54
|
|
55
|
+
raise StandardError, "#{response.code} #{response.body}" unless success_code?(response.code)
|
56
|
+
|
52
57
|
if as == :file
|
53
58
|
generate_tempfile(filename, response.body)
|
54
59
|
else
|
@@ -59,6 +64,28 @@ class BunnyStorageClient
|
|
59
64
|
nil
|
60
65
|
end
|
61
66
|
|
67
|
+
def exist?(storage_zone_name: nil, filename: nil)
|
68
|
+
storage_zone_name ||= @storage_zone_name
|
69
|
+
filename ||= @filename
|
70
|
+
|
71
|
+
uri = build_uri(storage_zone_name, filename)
|
72
|
+
|
73
|
+
# First, send a GET request to check if the file exists
|
74
|
+
# Maybe HEAD will be available in the future
|
75
|
+
head_request = Net::HTTP::Get.new(uri)
|
76
|
+
head_request['AccessKey'] = @access_key
|
77
|
+
head_request['accept'] = '*/*'
|
78
|
+
|
79
|
+
head_response = make_request(uri, head_request)
|
80
|
+
|
81
|
+
# Check if the file exists (status code 200)
|
82
|
+
if head_response.code != '200'
|
83
|
+
return false
|
84
|
+
end
|
85
|
+
|
86
|
+
true
|
87
|
+
end
|
88
|
+
|
62
89
|
# Uploads a file to BunnyCDN storage.
|
63
90
|
#
|
64
91
|
# @param storage_zone_name [String, nil] the storage zone name
|
@@ -72,8 +99,13 @@ class BunnyStorageClient
|
|
72
99
|
request['AccessKey'] = @access_key
|
73
100
|
request['content-type'] = 'application/octet-stream'
|
74
101
|
|
75
|
-
|
76
|
-
|
102
|
+
if body.respond_to?(:read)
|
103
|
+
body.rewind if body.respond_to?(:rewind) # Reset to the beginning if needed
|
104
|
+
request.body = body.read
|
105
|
+
else
|
106
|
+
request.body = body.to_s # Default to converting the body to a string
|
107
|
+
end
|
108
|
+
|
77
109
|
response = make_request(uri, request)
|
78
110
|
|
79
111
|
raise StandardError, "Response code #{response.code} is not OK!" unless success_code?(response.code)
|
@@ -94,7 +126,9 @@ class BunnyStorageClient
|
|
94
126
|
request = Net::HTTP::Delete.new(uri)
|
95
127
|
request['AccessKey'] = @access_key
|
96
128
|
response = make_request(uri, request)
|
97
|
-
|
129
|
+
|
130
|
+
# hack, 500 is also not found
|
131
|
+
raise StandardError, "Response code #{response.code} is not OK!" if !success_code?(response.code) && response.code != "404" && response.code != "500"
|
98
132
|
|
99
133
|
true
|
100
134
|
rescue StandardError => e
|
@@ -125,7 +159,7 @@ class BunnyStorageClient
|
|
125
159
|
private
|
126
160
|
|
127
161
|
def build_uri(storage_zone_name, filename)
|
128
|
-
url = File.join(
|
162
|
+
url = File.join(@base_url, storage_zone_name, filename)
|
129
163
|
url = url[...-1] if url.end_with?(File::SEPARATOR)
|
130
164
|
URI(url)
|
131
165
|
end
|
@@ -133,6 +167,8 @@ class BunnyStorageClient
|
|
133
167
|
def make_request(uri, request)
|
134
168
|
https = Net::HTTP.new(uri.host, uri.port)
|
135
169
|
https.use_ssl = true
|
170
|
+
https.open_timeout = 3
|
171
|
+
https.read_timeout = 5
|
136
172
|
https.request(request)
|
137
173
|
end
|
138
174
|
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bunny_storage_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramit Koul
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 0.1.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.1.0
|
27
27
|
description: BunnyStorage Client is a Ruby SDK for interacting with BunnyCDN storage
|