Class: Gcloud::Storage::File
- Inherits:
-
Object
- Object
- Gcloud::Storage::File
- Defined in:
- lib/gcloud/storage/file.rb,
lib/gcloud/storage/file/acl.rb,
lib/gcloud/storage/file/list.rb,
lib/gcloud/storage/file/verifier.rb
Overview
Represents the File/Object that belong to a Bucket.
Defined Under Namespace
Modules: Verifier Classes: Acl, List, Signer
Instance Attribute Summary collapse
-
#connection ⇒ Object
The Connection object.
-
#gapi ⇒ Object
The Google API Client object.
Class Method Summary collapse
-
.from_gapi(gapi, conn) ⇒ Object
New File from a Google API Client object.
Instance Method Summary collapse
-
#acl ⇒ Object
Access Control List.
-
#bucket ⇒ Object
The name of the bucket containing this file.
-
#copy(dest_bucket_or_path, dest_path = nil, options = {}) ⇒ Object
Copy the file to a new location.
-
#crc32c ⇒ Object
CRC32c checksum, as described in RFC 4960, Appendix B; encoded using base64.
-
#delete ⇒ Object
Permenently deletes the file.
-
#download(path, options = {}) ⇒ Object
Download the file’s contents to a local file.
-
#etag ⇒ Object
HTTP 1.1 Entity tag for the file.
-
#generation ⇒ Object
The content generation of this file.
-
#id ⇒ Object
The ID of the file.
-
#initialize ⇒ File
constructor
Create an empty File object.
-
#kind ⇒ Object
The kind of item this is.
-
#md5 ⇒ Object
MD5 hash of the data; encoded using base64.
-
#metageneration ⇒ Object
The version of the metadata for this file at this generation.
-
#name ⇒ Object
The name of this file.
- #signed_url(options = {}) ⇒ Object
-
#size ⇒ Object
Content-Length of the data in bytes.
-
#updated_at ⇒ Object
The creation or modification time of the file.
-
#url ⇒ Object
The url to the file.
Constructor Details
#initialize ⇒ File
Create an empty File object.
34 35 36 37 |
# File 'lib/gcloud/storage/file.rb', line 34 def initialize @connection = nil @gapi = {} end |
Instance Attribute Details
#connection ⇒ Object
The Connection object.
26 27 28 |
# File 'lib/gcloud/storage/file.rb', line 26 def connection @connection end |
#gapi ⇒ Object
The Google API Client object.
30 31 32 |
# File 'lib/gcloud/storage/file.rb', line 30 def gapi @gapi end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
New File from a Google API Client object.
201 202 203 204 205 206 |
# File 'lib/gcloud/storage/file.rb', line 201 def self.from_gapi gapi, conn #:nodoc: new.tap do |f| f.gapi = gapi f.connection = conn end end |
Instance Method Details
#acl ⇒ Object
Access Control List
195 196 197 |
# File 'lib/gcloud/storage/file.rb', line 195 def acl @acl ||= File::Acl.new self end |
#bucket ⇒ Object
The name of the bucket containing this file.
60 61 62 |
# File 'lib/gcloud/storage/file.rb', line 60 def bucket @gapi["bucket"] end |
#copy(dest_bucket_or_path, dest_path = nil, options = {}) ⇒ Object
Copy the file to a new location.
file.copy "path/to/destination/file.ext"
The file can also be copied to a different bucket:
file.copy "new-destination-bucket",
"path/to/destination/file.ext"
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/gcloud/storage/file.rb', line 161 def copy dest_bucket_or_path, dest_path = nil, = {} ensure_connection! dest_bucket, dest_path, = fix_copy_args dest_bucket_or_path, dest_path, resp = connection.copy_file bucket, name, dest_bucket, dest_path, if resp.success? File.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#crc32c ⇒ Object
CRC32c checksum, as described in RFC 4960, Appendix B; encoded using base64.
109 110 111 |
# File 'lib/gcloud/storage/file.rb', line 109 def crc32c @gapi["crc32c"] end |
#delete ⇒ Object
Permenently deletes the file.
177 178 179 180 181 182 183 184 185 |
# File 'lib/gcloud/storage/file.rb', line 177 def delete ensure_connection! resp = connection.delete_file bucket, name if resp.success? true else fail ApiError.from_response(resp) end end |
#download(path, options = {}) ⇒ Object
Download the file’s contents to a local file. The path provided must be writable.
file.download "path/to/downloaded/file.ext"
The download is verified by calculating the MD5 digest. The CRC32c digest can be used by passing :crc32c.
file.download "path/to/downloaded/file.ext", verify: :crc32c
Both the MD5 and CRC32c digest can be used by passing :all.
file.download "path/to/downloaded/file.ext", verify: :all
The download verification can be disabled by passing :none
file.download "path/to/downloaded/file.ext", verify: :none
If the verification fails FileVerificationError is raised.
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/gcloud/storage/file.rb', line 139 def download path, = {} ensure_connection! resp = connection.download_file bucket, name if resp.success? ::File.open path, "w+" do |f| f.write resp.body end verify_file! ::File.new(path), else fail ApiError.from_response(resp) end end |
#etag ⇒ Object
HTTP 1.1 Entity tag for the file.
115 116 117 |
# File 'lib/gcloud/storage/file.rb', line 115 def etag @gapi["etag"] end |
#generation ⇒ Object
The content generation of this file. Used for object versioning.
67 68 69 |
# File 'lib/gcloud/storage/file.rb', line 67 def generation @gapi["generation"] end |
#id ⇒ Object
The ID of the file.
48 49 50 |
# File 'lib/gcloud/storage/file.rb', line 48 def id @gapi["id"] end |
#kind ⇒ Object
The kind of item this is. For files, this is always storage#object.
42 43 44 |
# File 'lib/gcloud/storage/file.rb', line 42 def kind @gapi["kind"] end |
#md5 ⇒ Object
MD5 hash of the data; encoded using base64.
102 103 104 |
# File 'lib/gcloud/storage/file.rb', line 102 def md5 @gapi["md5Hash"] end |
#metageneration ⇒ Object
The version of the metadata for this file at this generation. Used for preconditions and for detecting changes in metadata. A metageneration number is only meaningful in the context of a particular generation of a particular file.
76 77 78 |
# File 'lib/gcloud/storage/file.rb', line 76 def @gapi["metageneration"] end |
#name ⇒ Object
The name of this file.
54 55 56 |
# File 'lib/gcloud/storage/file.rb', line 54 def name @gapi["name"] end |
#signed_url(options = {}) ⇒ Object
187 188 189 190 191 |
# File 'lib/gcloud/storage/file.rb', line 187 def signed_url = {} ensure_connection! signer = File::Signer.new self signer.signed_url end |
#size ⇒ Object
Content-Length of the data in bytes.
88 89 90 |
# File 'lib/gcloud/storage/file.rb', line 88 def size @gapi["size"] end |
#updated_at ⇒ Object
The creation or modification time of the file. For buckets with versioning enabled, changing an object’s metadata does not change this property.
96 97 98 |
# File 'lib/gcloud/storage/file.rb', line 96 def updated_at @gapi["updated"] end |
#url ⇒ Object
The url to the file.
82 83 84 |
# File 'lib/gcloud/storage/file.rb', line 82 def url @gapi["selfLink"] end |