Stay organized with collections
Save and categorize content based on your preferences.
Source code for google.appengine.api.blobstore.dict_blob_storage
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""In-memory implementation of Blobstore stub storage.
This module contains an implementation of `blob_storage.BlobStorage`.
"""
import StringIO
from google.appengine.api import blobstore
from google.appengine.api.blobstore import blob_storage
[docs]class DictBlobStorage(blob_storage.BlobStorage):
"""Stores blobs in a dictionary."""
def __init__(self):
"""Constructor."""
self._blobs = {}
[docs] def StoreBlob(self, blob_key, blob_stream):
"""Stores a blob stream."""
content = StringIO.StringIO()
try:
while True:
block = blob_stream.read(1 << 20)
if not block:
break
content.write(block)
self.CreateBlob(blob_key, content.getvalue())
finally:
content.close()
[docs] def CreateBlob(self, blob_key, blob):
"""Stores a blob in a map."""
self._blobs[blobstore.BlobKey(unicode(blob_key))] = blob
[docs] def OpenBlob(self, blob_key):
"""Gets the blob contents as a stream."""
return StringIO.StringIO(
self._blobs[blobstore.BlobKey(unicode(blob_key))])
[docs] def DeleteBlob(self, blob_key):
"""Deletes blob content."""
try:
del self._blobs[blobstore.BlobKey(unicode(blob_key))]
except KeyError:
pass
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-04-04 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-04-04 UTC."],[[["This code provides an in-memory implementation of `blob_storage.BlobStorage`, designed for storing blobs within a dictionary."],["The `DictBlobStorage` class is defined, which is responsible for managing the storage, creation, retrieval, and deletion of blobs."],["The `StoreBlob` method is used to save a blob stream, reading it in blocks and creating the final blob content."],["The `CreateBlob` method is responsible for saving the blob data with the associated blob key."],["The `OpenBlob` method is responsible for returning a stream with the blob content to be read, while the `DeleteBlob` method removes the content associated with the given key."]]],[]]