google.appengine.ext.blobstore.blobstore module
Summary
A Python blobstore API used by app developers.
This module contains methods that are used to interface with Blobstore API.
The module includes a db.Model
-like class that represents a reference to a
very large blob. The module imports a db.Key
-like class that represents a blob
key.
Contents
- exception google.appengine.ext.blobstore.blobstore.BlobFetchSizeTooLargeErrorsource
-
Bases: google.appengine.api.blobstore.blobstore.Error
The block could not be fetched because it was too large.
- class google.appengine.ext.blobstore.blobstore.BlobInfo(entity_or_blob_key, _values=None)source
-
Bases: object
Information about blobs in Blobstore.
This is a
db.Model
-like class that contains information about blobs that are stored by an application. Likedb.Model
, this class is backed by a Datastore entity; however,BlobInfo
instances are read-only and have a much more limited interface.Each
BlobInfo
has a key of typeBlobKey
associated with it. This key is specific to the Blobstore API and is not compatible withdb.get
. The key can be used for quick lookup by passing it toBlobInfo.get
. This key converts easily to a string, which is web safe and can be embedded in URLs.- Properties:
-
-
content_type: The content type of the blob.
-
creation: The creation date of the blob, or when it was uploaded.
-
filename: The file name that the user selected from their machine.
-
size: The size of the uncompressed blob.
-
md5_hash: The MD5 hash value of the uploaded blob.
- gs_object_name: The name of the object, if the blob is stored in
-
Google Cloud Storage, in the form /[bucket-name]/[object-name]
-
All properties are read-only. Attempting to assign a value to a property will raise a
NotImplementedError
.- classmethod all()source
Creates a query for all
ReturnsBlobInfo
objects associated with the app.A
db.Query
object that queries overBlobInfo
’s datastore kind.
- content_type
Returns the content type of the blob.
ReturnsThe content type of the blob.
- creation
Returns the creation date or time of upload of the blob.
ReturnsThe creation date or time of upload of the blob.
- delete(_token=None)source
-
Permanently deletes a blob from Blobstore.
- filename
Returns the file name that the user selected from their machine.
ReturnsThe file name that the user selected.
- classmethod from_entity(entity)source
Converts an entity to
ParametersBlobInfo
.entity – The entity that you are trying to convert.
ReturnsThe
BlobInfo
that was converted from the entity.
- classmethod get(blob_keys)source
Retrieves a
ParametersBlobInfo
by key or by a list of keys.blob_keys – A key or a list of keys. Keys can be in string, Unicode, or
ReturnsBlobKey
format.A
BlobInfo
instance that is associated with the provided key or a list ofBlobInfo
instances if a list of keys was provided. Keys that are not found in Blobstore returnNone
.
- classmethod gql(query_string, *args, **kwds)source
Returns a query using a GQL query string.
See the
ParametersGQL source
for more information about GQL.-
query_string – A properly formatted GQL query string that omits
SELECT * FROM <entity>
-
*args – The remaining positional arguments that are used to bind numeric references in the query.
-
**kwds – The dictionary-based arguments for named parameters.
A
gql.GqlQuery
object that queries overBlobInfo
’s datastore kind.-
- gs_object_name
- key()source
Gets the key for a blob.
ReturnsThe
BlobKey
instance that identifies this blob.
- classmethod kind()source
Gets the entity kind for the
ReturnsBlobInfo
.The entity kind for
BlobInfo
.
- md5_hash
Returns the MD5 hash value of the uncompressed blob.
ReturnsThe hash value of the uncompressed blob.
- open(*args, **kwargs)source
Returns a
ParametersBlobReader
for this blob.-
*args – Arguments to pass to the
BlobReader
constructor. -
**kwargs – Keyword arguments to pass to the
BlobReader
constructor.
A
BlobReader
instance.-
- classmethod properties()source
Defines the set of properties that belong to
ReturnsBlobInfo
.A set of all of the properties that belong to
BlobInfo
.
- size
Returns the size of the uncompressed blob.
ReturnsThe size of the uncompressed blob.
- exception google.appengine.ext.blobstore.blobstore.BlobInfoParseErrorsource
-
Bases: google.appengine.api.blobstore.blobstore.Error
The CGI parameter does not contain a valid
BlobInfo
record.
- class google.appengine.ext.blobstore.blobstore.BlobKey(blob_key)source
-
Bases: object
Key used to identify a blob in Blobstore.
This object wraps a string that gets used internally by the Blobstore API to identify application blobs. The BlobKey corresponds to the entity name of the underlying BlobReference entity.
This class is exposed in the API in both google.appengine.ext.db and google.appengine.ext.blobstore.
- ToXml()source
- class google.appengine.ext.blobstore.blobstore.BlobMigrationRecord(parent=None, key_name=None, _app=None, _from_entity=False, **kwds)source
-
Bases: google.appengine.ext.db.Model
Defines a model that records the result of a blob migration.
- classmethod get_by_blob_key(old_blob_key)source
Fetches the
ParametersBlobMigrationRecord
for the given blob key.old_blob_key – The blob key that was used in the previous app.
ReturnsA instance of
blobstore.BlobMigrationRecord
orNone
.
- classmethod get_new_blob_key(old_blob_key)source
Looks up the new key for a blob.
Parametersold_blob_key – The original blob key.
ReturnsThe
blobstore.BlobKey
of the migrated blob.
- classmethod kind()source
Specifies the kind of blob that you are migrating.
ReturnsThe kind of blob that you are migrating
- new_blob_ref
-
Property compatible with
db.Model
classes.Add references to blobs to domain models using BlobReferenceProperty:
class Picture(db.Model): title = db.StringProperty() image = blobstore.BlobReferenceProperty() thumbnail = blobstore.BlobReferenceProperty()
To find the size of a picture using this model:
picture = Picture.get(picture_key) print picture.image.size
BlobInfo
objects are lazily loaded, so iterating over models forBlobKeys
is efficient. The following sample code does not need to hit Datastore for each image key:list_of_untitled_blobs = [] for picture in Picture.gql("WHERE title=''"): list_of_untitled_blobs.append(picture.image.key())
- exception google.appengine.ext.blobstore.blobstore.BlobNotFoundErrorsource
-
Bases: google.appengine.api.blobstore.blobstore.Error
The blob does not exist.
- class google.appengine.ext.blobstore.blobstore.BlobReferenceProperty(verbose_name=None, name=None, default=None, required=False, validator=None, choices=None, indexed=True)source
-
Bases: google.appengine.ext.db.Property
Property compatible with
db.Model
classes.Add references to blobs to domain models using BlobReferenceProperty:
class Picture(db.Model): title = db.StringProperty() image = blobstore.BlobReferenceProperty() thumbnail = blobstore.BlobReferenceProperty()
To find the size of a picture using this model:
picture = Picture.get(picture_key) print picture.image.size
BlobInfo
objects are lazily loaded, so iterating over models forBlobKeys
is efficient. The following sample code does not need to hit Datastore for each image key:list_of_untitled_blobs = [] for picture in Picture.gql("WHERE title=''"): list_of_untitled_blobs.append(picture.image.key())
- data_type
-
alias of BlobInfo
- get_value_for_datastore(model_instance)source
Returns a model property translated to a datastore value.
Parametersmodel_instance – The model property that you want to translate.
ReturnsThe model property that was translated from datastore.
- make_value_from_datastore(value)source
Returns a datastore value to
ParametersBlobInfo
.value – The datastore value that you want to translate.
ReturnsA
BlobInfo
that was translated from datastore.
- validate(value)source
Validates that an assigned value is
BlobInfo
.This method automatically converts from strings and
ParametersBlobKey
instances.value – The value that you want to validate.
ReturnsInformation about whether an assigned value is
BlobInfo
.
- class google.appengine.ext.blobstore.blobstore.BlobReader(blob, buffer_size=131072, position=0)source
-
Bases: object
Provides a read-only file-like interface to a blobstore blob.
- SEEK_CUR = 1
- SEEK_END = 2
- SEEK_SET = 0
- blob_info
Returns the
ReturnsBlobInfo
for this file.A string that contains the
BlobInfo
.
- close()source
-
Closes the file.
A closed file cannot be read or written to anymore. An operation that requires that the file to be open will raise a
ValueError
after the file has been closed. Callingclose()
more than once is allowed.
- closed
Determines whether a file is closed.
ReturnsTrue
if this file is closed; opened files returnFalse
.
- flush()source
- next()source
Returns the next line from the file.
ReturnsA string, terminated by
Raises\n
. The last line cannot end with\n
. If the end of the file is reached, an empty string will be returned.StopIteration – If there are no further lines to read.
- read(size=-1)source
Reads at most
size
bytes from the file.Fewer bytes are read if the read hits the end of the file before obtaining
size
bytes. If thesize
argument is negative or omitted, all data is read until the end of the file is reached. The bytes are returned as a string object. An empty string is returned immediately when the end of the file is reached.Calling
Parametersread()
without specifying asize
is likely to be dangerous, as it might read excessive amounts of data.size – Optional. The maximum number of bytes to read. When omitted,
Returnsread()
returns all remaining data in the file.The read data, as a string.
- readline(size=-1)source
Reads one entire line from the file.
A trailing newline character is kept in the string but can be absent when a file ends with an incomplete line. If the
Parameterssize
argument is present and non-negative, it represents a maximum byte count, including the trailing newline and an incomplete line might be returned. An empty string is returned immediately only when the end of the file is reached.size – Optional. The maximum number of bytes to read.
ReturnsThe read data, as a string.
- readlines(sizehint=None)source
Reads until the end of the file using
readline()
.A list of lines read is returned.
If the optional
Parameterssizehint
argument is present, instead of reading up to the end of the file, whole lines totalling approximatelysizehint
bytes are read, possibly after rounding up to an internal buffer size.sizehint – A hint as to the maximum number of bytes to read.
ReturnsA list of strings, each being a single line from the file.
- seek(offset, whence=0)source
Sets the file’s current position, like stdio’s fseek().
Parameters-
offset – The relative offset to seek to.
-
whence – Optional; defines to what value the offset is relative. This argument defaults to
os.SEEK_SET
or0
to use absolute file positioning; other valid values areos.SEEK_CUR
or1
to seek relative to the current position andos.SEEK_END
or2
to seek relative to the end of the file.
-
- truncate(size)source
Raises an error if you attempt to truncate the file.
Parameterssize – The size that you are attempting to truncate to.
RaisesIOError – If you attempt to truncate a file in
BlobReader
.
- write(str)source
Raises an error if you attempt to write to the file.
Parametersstr – The string that you are attempting to write.
RaisesIOError – If you attempt to write to a file in
BlobReader
.
- writelines(sequence)source
Raises an error if you attempt to write to the file.
Parameterssequence – The sequence of strings that you are attempting to write.
RaisesIOError – If you attempt to write lines to a file in
BlobReader
.
- class google.appengine.ext.blobstore.blobstore.FileInfo(filename=None, content_type=None, creation=None, size=None, md5_hash=None, gs_object_name=None)source
-
Bases: object
Details information about uploaded files.
This class contains information about blobs stored by an application.
This class is similar to
BlobInfo
; however, this method does not make use of a key, and the information is not persisted in the datastore.- Properties:
-
-
content_type: The content type of the uploaded file.
-
- creation: The creation date of the uploaded file, or when it was
-
uploaded.
-
filename: The file name that the user selected from their machine.
-
size: The size of the uncompressed file.
-
md5_hash: The MD5 hash value of the uploaded file.
-
gs_object_name: The name of the file that was written to Google Cloud Storage, or
None
if the file was not uploaded to Google Cloud Storage.
-
All properties are read-only. Attempting to assign a value to a property will raise an
AttributeError
.- content_type
Returns the content type of the uploaded file.
ReturnsThe content type of the file.
- creation
Returns the creation date or upload time of the file.
ReturnsThe creation date or upload time of the file.
- filename
Returns the file name that the user selected.
ReturnsThe file name that the user selected.
- gs_object_name
Returns the name of the file that was written to Cloud Storage.
ReturnsThe name of the file that was written to Cloud Storage.
- md5_hash
Returns the MD5 hash of the uploaded file.
ReturnsThe hash value for the uploaded file.
- size
Returns the size of the uncompressed file.
ReturnsThe size of the uncompressed file.
- exception google.appengine.ext.blobstore.blobstore.FileInfoParseErrorsource
-
Bases: google.appengine.api.blobstore.blobstore.Error
The CGI parameter does not contain a valid
FileInfo
record.
- exception google.appengine.ext.blobstore.blobstore.DataIndexOutOfRangeErrorsource
-
Bases: google.appengine.api.blobstore.blobstore.Error
The indexes could not be accessed.
The specified indexes were out of range or in the wrong order.
- exception google.appengine.ext.blobstore.blobstore.PermissionDeniedErrorsource
-
Bases: google.appengine.api.blobstore.blobstore.Error
The operation did not complete; review the permissions required.
- exception google.appengine.ext.blobstore.blobstore.Errorsource
-
Bases: exceptions.Exception
Base blobstore error type.
- exception google.appengine.ext.blobstore.blobstore.InternalErrorsource
-
Bases: google.appengine.api.blobstore.blobstore.Error
An internal error occured.
- google.appengine.ext.blobstore.blobstore.create_rpc(deadline=None, callback=None)source
Creates an RPC object to use with the Blobstore API.
Parameters-
deadline – Optional deadline in seconds for the operation; the default value is a system-specific deadline, typically 5 seconds.
-
callback – Optional callable to invoke on completion.
An
apiproxy_stub_map.UserRPC
object that is specialized for this service.-
- google.appengine.ext.blobstore.blobstore.create_upload_url(success_path, max_bytes_per_blob=None, max_bytes_total=None, rpc=None, gs_bucket_name=None)source
Creates the upload URL for a POST form.
Parameters-
success_path – Path within application to call when a
POST
call is successful and the upload is complete. -
max_bytes_per_blob – The maximum size in bytes that any one blob in the upload can be, or
None
for no maximum size. -
max_bytes_total – The maximum size in bytes that the aggregate sizes of all of the blobs in the upload can be, or
None
for no maximum size. -
rpc – Optional UserRPC object.
-
gs_bucket_name – The Google Cloud Storage bucket name to which the blobs should be uploaded. The application’s service account must have the correct permissions to write to this bucket. The bucket name can be of the format
bucket/path/
, in which case the included path will be prepended to the uploaded object name.
The upload URL.
Raises-
TypeError – If
max_bytes_per_blob
ormax_bytes_total
are not integral types. -
ValueError – If
max_bytes_per_blob
ormax_bytes_total
are not positive values.
-
- google.appengine.ext.blobstore.blobstore.create_upload_url_async(success_path, max_bytes_per_blob=None, max_bytes_total=None, rpc=None, gs_bucket_name=None)source
Asynchronously creates the upload URL for a POST form.
Parameters-
success_path – The path within the application to call when a
POST
call is successful and the upload is complete. -
max_bytes_per_blob – The maximum size in bytes that any one blob in the upload can be, or
None
for no maximum size. -
max_bytes_total – The maximum size in bytes that the aggregate sizes of all of the blobs in the upload can be, or
None
for no maximum size. -
rpc – Optional UserRPC object.
-
gs_bucket_name – The Google Cloud Storage bucket name to which the blobs should be uploaded. The application’s service account must have the correct permissions to write to this bucket. The bucket name can be of the format
bucket/path/
, in which case the included path will be prepended to the uploaded object name.
A UserRPC whose result will be the upload URL.
Raises-
TypeError – If
max_bytes_per_blob
ormax_bytes_total
are not integral types. -
ValueError – If
max_bytes_per_blob
ormax_bytes_total
are not positive values.
-
- google.appengine.ext.blobstore.blobstore.delete(blob_keys, rpc=None, _token=None)source
Deletes a blob from Blobstore.
Parameters-
blob_keys – A single
BlobKey
instance or a list of blob keys. A blob key can be either a string or an instance ofBlobKey
. -
rpc – Optional UserRPC object.
None.
-
- google.appengine.ext.blobstore.blobstore.delete_async(blob_keys, rpc=None, _token=None)source
Asynchronously deletes a blob from Blobstore.
Parameters-
blob_keys – A single
BlobKey
instance or a list of blob keys. A blob key can be either a string or an instance ofBlobKey
. -
rpc – Optional UserRPC object.
A UserRPC, whose result will be
None
.-
- google.appengine.ext.blobstore.blobstore.fetch_data(blob, start_index, end_index, rpc=None)source
Fetches data for a blob.
Fetches a fragment of a blob up to
ParametersMAX_BLOB_FETCH_SIZE
in length. Attempting to fetch a fragment that extends beyond the boundaries of the blob will return the amount of data fromstart_index
until the end of the blob, which will be a smaller size than requested. Requesting a fragment that is entirely outside the boundaries of the blob will return an empty string. Attempting to fetch a negative index will raise an exception.-
blob – A
BlobInfo
,BlobKey
, string, or Unicode representation of theBlobKey
of the blob from which you want to fetch data. -
start_index – The start index of blob data to fetch. This value must not be negative.
-
end_index – The end index (inclusive) of the blob data to fetch. This value must be greater than or equal to
start_index
. -
rpc – Optional UserRPC object.
A string that contains partial data of a blob. If the indexes are legal but outside of the boundaries of the blob, an empty string is returned.
Raises-
TypeError – If
start_index
orend_index
are not indexes, or ifblob
is not a string,BlobKey
orBlobInfo
. -
DataIndexOutOfRangeError – If
start_index
is set to a value that is less than 0 orend_index
is less thanstart_index
. -
BlobFetchSizeTooLargeError – If the requested blob fragment is larger than
MAX_BLOB_FETCH_SIZE
. -
BlobNotFoundError – If the blob does not exist.
-
- google.appengine.ext.blobstore.blobstore.fetch_data_async(blob, start_index, end_index, rpc=None)source
Asynchronously fetches data for a blob.
Fetches a fragment of a blob up to
ParametersMAX_BLOB_FETCH_SIZE
in length. Attempting to fetch a fragment that extends beyond the boundaries of the blob will return the amount of data fromstart_index
until the end of the blob, which will be a smaller size than requested. Requesting a fragment that is entirely outside the boundaries of the blob will return an empty string. Attempting to fetch a negative index will raise an exception.-
blob – A
BlobInfo
,BlobKey
, string, or Unicode representation of theBlobKey
of the blob from which you want to fetch data. -
start_index – The start index of blob data to fetch. This value must not be negative.
-
end_index – The end index (inclusive) of the blob data to fetch. This value must be greater than or equal to
start_index
. -
rpc – Optional UserRPC object.
A UserRPC whose result will be a string as returned by
Raisesfetch_data()
.-
TypeError – If
start_index
orend_index
are not indexes, or ifblob
is not a string,BlobKey
orBlobInfo
. -
DataIndexOutOfRangeError – If
start_index
is set to a value that is less than 0 orend_index
is less thanstart_index
when callingrpc.get_result()
. -
BlobFetchSizeTooLargeError – If the requested blob fragment is larger than
MAX_BLOB_FETCH_SIZE
when callingrpc.get_result()
. -
BlobNotFoundError – If the blob does not exist when calling
rpc.get_result()
.
-
- google.appengine.ext.blobstore.blobstore.create_gs_key(filename, rpc=None)source
Creates an encoded key for a Google Cloud Storage file.
It is safe to persist this key for future use.
Parameters-
filename – The file name of the Google Cloud Storage object for which you want to create the key.
-
rpc – Optional UserRPC object.
An encrypted
BlobKey
string.-
- google.appengine.ext.blobstore.blobstore.create_gs_key_async(filename, rpc=None)source
Asynchronously creates an encoded key for a Google Cloud Storage file.
It is safe to persist this key for future use.
Parameters-
filename – The file name of the Google Cloud Storage object for which you want to create the key.
-
rpc – Optional UserRPC object.
A UserRPC whose result will be a string as returned by
Raisescreate_gs_key()
.-
TypeError – If
filename
is not a string. -
ValueError – If
filename
is not in the format/gs/bucket_name/object_name
.
-
- google.appengine.ext.blobstore.blobstore.get(blob_key)source
Gets a
BlobInfo
record from blobstore.Does the same as
ParametersBlobInfo.get
.blob_key – The
ReturnsBlobKey
of the record you want to retrieve.A
BlobInfo
instance that is associated with the provided key or a list ofBlobInfo
instances if a list of keys was provided. Keys that are not found in Blobstore returnNone
.
- google.appengine.ext.blobstore.blobstore.parse_blob_info(field_storage)source
Parses a
ParametersBlobInfo
record from file uploadfield_storage
.field_storage –
Returnscgi.FieldStorage
that represents an uploaded blob.A
RaisesBlobInfo
record as parsed from thefield_storage
instance. This method will returnNone
iffield_storage
was not defined.BlobInfoParseError – If the provided
field_storage
does not contain enough information to construct aBlobInfo
object.
- google.appengine.ext.blobstore.blobstore.parse_file_info(field_storage)source
Parses a
ParametersFileInfo
record from file uploadfield_storage
.field_storage –
Returnscgi.FieldStorage
that represents the uploaded file.
RaisesFileInfo
record as parsed from thefield_storage
instance. This method will returnNone
iffield_storage
was not specified.FileInfoParseError – If
field_storage
does not contain enough information to construct aFileInfo
object.