OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import blob_reference_store as datastore |
| 6 from blob_reference_store import BlobReferenceStore |
| 7 from appengine_wrappers import blobstore |
| 8 from appengine_wrappers import files |
| 9 |
| 10 BLOBSTORE_GITHUB = 'BlobstoreGithub' |
| 11 |
| 12 class AppEngineBlobstore(object): |
| 13 """A wrapper around the blobstore API, which stores the blob keys in |
| 14 datastore. |
| 15 """ |
| 16 def __init__(self): |
| 17 self._datastore = BlobReferenceStore('blobstore') |
| 18 |
| 19 def Set(self, key, blob, namespace): |
| 20 """Add a blob to the blobstore. |version| is used as part of the key so |
| 21 multiple blobs with the same name can be differentiated. |
| 22 """ |
| 23 key = namespace + '.' + key |
| 24 filename = files.blobstore.create() |
| 25 with files.open(filename, 'a') as f: |
| 26 f.write(blob) |
| 27 files.finalize(filename) |
| 28 blob_key = files.blobstore.get_blob_key(filename) |
| 29 self._datastore.Set(datastore.BLOB_REFERENCE_BLOBSTORE, key, blob_key) |
| 30 |
| 31 def Get(self, key, namespace): |
| 32 """Get a blob with version |version|. |
| 33 """ |
| 34 key = namespace + '.' + key |
| 35 blob_key = self._datastore.Get(datastore.BLOB_REFERENCE_BLOBSTORE, key) |
| 36 if blob_key is None: |
| 37 return None |
| 38 blob_reader = blobstore.BlobReader(blob_key) |
| 39 return blob_reader.read() |
| 40 |
| 41 def Delete(self, key, namespace): |
| 42 """Delete the blob with version |version| if it is found. |
| 43 """ |
| 44 key = namespace + '.' + key |
| 45 blob_key = self._datastore.Delete(datastore.BLOB_REFERENCE_BLOBSTORE, key) |
| 46 if blob_key is None: |
| 47 return |
| 48 blob_key.delete() |
OLD | NEW |