예제 #1
0
    def fetch_attachment(self, name, stream=False):
        """Get named attachment

        :param stream: When true, return a file-like object that can be
        read at least once (streamers should not expect to seek within
        or read the contents of the returned file more than once).
        """
        db = get_blob_db()
        try:
            try:
                key = self.external_blobs[name].key
            except KeyError:
                if self._migrating_blobs_from_couch:
                    return super(BlobMixin, self) \
                        .fetch_attachment(name, stream=stream)
                raise NotFound(name)
            blob = db.get(key=key)
        except NotFound:
            raise ResourceNotFound(
                "{model} {model_id} attachment: {name!r}".format(
                    model=type(self).__name__,
                    model_id=self._id,
                    name=name,
                ))
        if stream:
            return blob

        with blob:
            return blob.read()
예제 #2
0
 def size(self, identifier=None, bucket=DEFAULT_BUCKET, key=None):
     if not (identifier is None and bucket == DEFAULT_BUCKET):
         # legacy: can be removed with old API
         assert key is None, key
         key = self.get_path(identifier, bucket)
     check_safe_key(key)
     with maybe_not_found(throw=NotFound(key)), self.report_timing('size', key):
         return self._s3_bucket().Object(key).content_length
예제 #3
0
파일: s3db.py 프로젝트: xbryanc/commcare-hq
 def exists(self, identifier, bucket=DEFAULT_BUCKET):
     path = self.get_path(identifier, bucket)
     try:
         with maybe_not_found(throw=NotFound(identifier, bucket)):
             self._s3_bucket().Object(path).load()
         return True
     except NotFound:
         return False
예제 #4
0
 def get(self, identifier=None, bucket=DEFAULT_BUCKET, key=None):
     if not (identifier is None and bucket == DEFAULT_BUCKET):
         # legacy: can be removed with old API
         assert key is None, key
         key = self.get_path(identifier, bucket)
     check_safe_key(key)
     with maybe_not_found(throw=NotFound(key)), self.report_timing('get', key):
         resp = self._s3_bucket().Object(key).get()
     return BlobStream(resp["Body"], self, key)
예제 #5
0
 def exists(self, key):
     check_safe_key(key)
     try:
         with maybe_not_found(throw=NotFound(key)), self.report_timing(
                 'exists', key):
             self._s3_bucket().Object(key).load()
         return True
     except NotFound:
         return False
예제 #6
0
 def size(self, identifier=None, bucket=DEFAULT_BUCKET, key=None):
     if identifier is None and bucket == DEFAULT_BUCKET:
         path = self.get_path(key=key)
     else:
         assert key is None, key
         key = join(bucket, identifier)
         path = self.get_path(identifier, bucket)
     if not exists(path):
         datadog_counter('commcare.blobdb.notfound')
         raise NotFound(key)
     return _count_size(path).size
예제 #7
0
 def exists(self, identifier=None, bucket=DEFAULT_BUCKET, key=None):
     if not (identifier is None and bucket == DEFAULT_BUCKET):
         # legacy: can be removed with old API
         assert key is None, key
         key = self.get_path(identifier, bucket)
     check_safe_key(key)
     try:
         with maybe_not_found(throw=NotFound(key)), self.report_timing('exists', key):
             self._s3_bucket().Object(key).load()
         return True
     except NotFound:
         return False
예제 #8
0
 def get(self, identifier=None, bucket=DEFAULT_BUCKET, key=None):
     if identifier is None and bucket == DEFAULT_BUCKET:
         path = self.get_path(key=key)
     else:
         # legacy: can be removed with old API
         assert key is None, key
         key = join(bucket, identifier)
         path = self.get_path(identifier, bucket)
     if not exists(path):
         datadog_counter('commcare.blobdb.notfound')
         raise NotFound(key)
     return open(path, "rb")
예제 #9
0
    def get(self, key=None, type_code=None, meta=None):
        key = self._validate_get_args(key, type_code, meta)
        path = self.get_path(key)
        if not exists(path):
            metrics_counter('commcare.blobdb.notfound')
            raise NotFound(key)

        file_obj = open(path, "rb")
        if meta and meta.is_compressed:
            content_length, compressed_length = meta.content_length, meta.compressed_length
            file_obj = GzipFile(fileobj=file_obj, mode='rb')
        else:
            content_length, compressed_length = self.size(key), None
        return BlobStream(file_obj, self, key, content_length, compressed_length)
예제 #10
0
    def get(self, key=None, type_code=None, meta=None):
        key = self._validate_get_args(key, type_code, meta)
        check_safe_key(key)
        with maybe_not_found(throw=NotFound(key)), self.report_timing(
                'get', key):
            resp = self._s3_bucket().Object(key).get()
        reported_content_length = resp['ContentLength']

        body = resp["Body"]
        if meta and meta.is_compressed:
            content_length, compressed_length = meta.content_length, meta.compressed_length
            body = GzipFile(key, mode='rb', fileobj=body)
        else:
            content_length, compressed_length = reported_content_length, None
        return BlobStream(body, self, key, content_length, compressed_length)
예제 #11
0
파일: mixin.py 프로젝트: lskdev/commcare-hq
    def fetch_attachment(self, name, stream=False, return_bytes=False):
        """Get named attachment

        :param stream: When true, return a file-like object that can be
        read at least once (streamers should not expect to seek within
        or read the contents of the returned file more than once).
        """
        db = get_blob_db()
        try:
            try:
                key = self.external_blobs[name].key
            except KeyError:
                if self._migrating_blobs_from_couch:
                    return super(BlobMixin, self) \
                        .fetch_attachment(name, stream=stream)
                raise NotFound(name)
            blob = db.get(key=key)
        except NotFound:
            raise ResourceNotFound(
                "{model} {model_id} attachment: {name!r}".format(
                    model=type(self).__name__,
                    model_id=self._id,
                    name=name,
                ))
        if stream:
            return blob

        with blob:
            body = blob.read()

        if return_bytes:
            return body

        try:
            body = body.decode("utf-8", "strict")
        except UnicodeDecodeError:
            # Return bytes on decode failure, otherwise unicode.
            # Ugly, but consistent with restkit.wrappers.Response.body_string
            pass
        return body
예제 #12
0
파일: fsdb.py 프로젝트: alemat/commcare-hq
 def get(self, identifier, bucket=DEFAULT_BUCKET):
     path = self.get_path(identifier, bucket)
     if not exists(path):
         raise NotFound(identifier, bucket)
     return open(path, "rb")
예제 #13
0
 def size(self, key):
     path = self.get_path(key)
     if not exists(path):
         metrics_counter('commcare.blobdb.notfound')
         raise NotFound(key)
     return _count_size(path).size
예제 #14
0
 def get(self, key):
     path = self.get_path(key)
     if not exists(path):
         metrics_counter('commcare.blobdb.notfound')
         raise NotFound(key)
     return open(path, "rb")
예제 #15
0
 def size(self, key):
     check_safe_key(key)
     with maybe_not_found(throw=NotFound(key)), self.report_timing(
             'size', key):
         return self._s3_bucket().Object(key).content_length
예제 #16
0
 def get(self, identifier, bucket=DEFAULT_BUCKET):
     path = self.get_path(identifier, bucket)
     with maybe_not_found(throw=NotFound(identifier, bucket)):
         resp = self._s3_bucket().Object(path).get()
     return BlobStream(resp["Body"], self, path)
예제 #17
0
 def size(self, identifier, bucket=DEFAULT_BUCKET):
     path = self.get_path(identifier, bucket)
     if not exists(path):
         datadog_counter('commcare.blobdb.notfound')
         raise NotFound(identifier, bucket)
     return os.path.getsize(path)
예제 #18
0
 def get(self, key):
     check_safe_key(key)
     with maybe_not_found(throw=NotFound(key)), self.report_timing(
             'get', key):
         resp = self._s3_bucket().Object(key).get()
     return BlobStream(resp["Body"], self, key)
예제 #19
0
 def get(self, identifier, bucket=DEFAULT_BUCKET):
     path = self.get_path(identifier, bucket)
     with maybe_not_found(throw=NotFound(identifier, bucket)):
         resp = self._s3_bucket().Object(path).get()
     return ClosingContextProxy(resp["Body"])  # body stream
예제 #20
0
파일: s3db.py 프로젝트: xbryanc/commcare-hq
 def size(self, identifier, bucket=DEFAULT_BUCKET):
     path = self.get_path(identifier, bucket)
     with maybe_not_found(throw=NotFound(identifier, bucket)):
         return self._s3_bucket().Object(path).content_length