Class: Gcloud::Storage::File

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFile

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

#connectionObject

The Connection object.



26
27
28
# File 'lib/gcloud/storage/file.rb', line 26

def connection
  @connection
end

#gapiObject

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

#aclObject

Access Control List



195
196
197
# File 'lib/gcloud/storage/file.rb', line 195

def acl
  @acl ||= File::Acl.new self
end

#bucketObject

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, options = {}
  ensure_connection!
  dest_bucket, dest_path, options = fix_copy_args dest_bucket_or_path,
                                                  dest_path, options

  resp = connection.copy_file bucket, name,
                              dest_bucket, dest_path, options
  if resp.success?
    File.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#crc32cObject

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

#deleteObject

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, options = {}
  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), options
  else
    fail ApiError.from_response(resp)
  end
end

#etagObject

HTTP 1.1 Entity tag for the file.



115
116
117
# File 'lib/gcloud/storage/file.rb', line 115

def etag
  @gapi["etag"]
end

#generationObject

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

#idObject

The ID of the file.



48
49
50
# File 'lib/gcloud/storage/file.rb', line 48

def id
  @gapi["id"]
end

#kindObject

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

#md5Object

MD5 hash of the data; encoded using base64.



102
103
104
# File 'lib/gcloud/storage/file.rb', line 102

def md5
  @gapi["md5Hash"]
end

#metagenerationObject

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 metageneration
  @gapi["metageneration"]
end

#nameObject

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 options = {}
  ensure_connection!
  signer = File::Signer.new self
  signer.signed_url options
end

#sizeObject

Content-Length of the data in bytes.



88
89
90
# File 'lib/gcloud/storage/file.rb', line 88

def size
  @gapi["size"]
end

#updated_atObject

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

#urlObject

The url to the file.



82
83
84
# File 'lib/gcloud/storage/file.rb', line 82

def url
  @gapi["selfLink"]
end