Пример #1
0
def fetch_data(blob_key,
               start_index,
               end_index,
               _make_sync_call=apiproxy_stub_map.MakeSyncCall):
    """Fetch data for blob.

  See docstring for ext.blobstore.fetch_data for more details.

  Args:
    blob: BlobKey, str or unicode representation of BlobKey of
      blob to fetch data from.
    start_index: Start index of blob data to fetch.  May not be negative.
    end_index: End index (exclusive) of blob data to fetch.  Must be
      >= start_index.

  Returns:
    str containing partial data of blob.  See docstring for
    ext.blobstore.fetch_data for more details.

  Raises:
    See docstring for ext.blobstore.fetch_data for more details.
  """
    if not isinstance(start_index, (int, long)):
        raise TypeError('start_index must be integer.')

    if not isinstance(end_index, (int, long)):
        raise TypeError('end_index must be integer.')

    if isinstance(blob_key, BlobKey):
        blob_key = str(blob_key).decode('utf-8')
    elif isinstance(blob_key, str):
        blob_key = blob_key.decode('utf-8')
    elif not isinstance(blob_key, unicode):
        raise TypeError('Blob-key must be str, unicode or BlobKey: %s' %
                        blob_key)

    if start_index < 0:
        raise DataIndexOutOfRangeError('May not fetch blob at negative index.')

    if end_index < start_index:
        raise DataIndexOutOfRangeError('Start index %d > end index %d' %
                                       (start_index, end_index))

    fetch_size = end_index - start_index + 1

    if fetch_size > MAX_BLOB_FETCH_SIZE:
        raise BlobFetchSizeTooLargeError('Blob fetch size is too large: %d' %
                                         fetch_size)

    request = blobstore_service_pb.FetchDataRequest()
    response = blobstore_service_pb.FetchDataResponse()

    request.set_blob_key(blob_key)
    request.set_start_index(start_index)
    request.set_end_index(end_index)

    try:
        _make_sync_call('blobstore', 'FetchData', request, response)
    except apiproxy_errors.ApplicationError, e:
        raise _ToBlobstoreError(e)
Пример #2
0
def fetch_data_async(blob_key, start_index, end_index, rpc=None):
  """Fetch data for blob -- async version.

  See docstring for ext.blobstore.fetch_data for more details.

  Args:
    blob: BlobKey, str or unicode representation of BlobKey of
      blob to fetch data from.
    start_index: Start index of blob data to fetch.  May not be negative.
    end_index: End index (exclusive) of blob data to fetch.  Must be
      >= start_index.
    rpc: Optional UserRPC object.

  Returns:
    A UserRPC whose result will be a str as returned by fetch_data().

  Raises:
    See docstring for ext.blobstore.fetch_data for more details.
  """
  if not isinstance(start_index, six.integer_types):
    raise TypeError('start_index must be integer.')

  if not isinstance(end_index, six.integer_types):
    raise TypeError('end_index must be integer.')

  if isinstance(blob_key, BlobKey):
    blob_key = str(blob_key).decode('utf-8')
  elif isinstance(blob_key, str):
    blob_key = blob_key.decode('utf-8')
  elif not isinstance(blob_key, six.text_type):
    raise TypeError('Blob-key must be str, unicode or BlobKey: %s' % blob_key)


  if start_index < 0:
    raise DataIndexOutOfRangeError(
        'May not fetch blob at negative index.')


  if end_index < start_index:
    raise DataIndexOutOfRangeError(
        'Start index %d > end index %d' % (start_index, end_index))


  fetch_size = end_index - start_index + 1

  if fetch_size > MAX_BLOB_FETCH_SIZE:
    raise BlobFetchSizeTooLargeError(
        'Blob fetch size is too large: %d' % fetch_size)

  request = blobstore_service_pb.FetchDataRequest()
  response = blobstore_service_pb.FetchDataResponse()

  request.set_blob_key(blob_key)
  request.set_start_index(start_index)
  request.set_end_index(end_index)

  return _make_async_call(rpc, 'FetchData', request, response,
                          _get_result_hook, lambda rpc: rpc.response.data())
Пример #3
0
def fetch_data_async(blob_key, start_index, end_index, rpc=None):
  """Asynchronously fetches the data for a blob.

  Args:
    blob_key: A `BlobKey`, string, or Unicode representation of a `BlobKey` of
        the blob from which you want to fetch data.
    start_index: The start index value of the blob data to fetch. This argument
        cannot be set to a negative value.
    end_index: The end index value (exclusive) of the blob data to fetch. This
        argument must be greater than or equal to the `start_index` value.
    rpc: Optional UserRPC object.

  Returns:
    A UserRPC whose result will be a string as returned by `fetch_data()`.
  """
  if not isinstance(start_index, (int, long)):
    raise TypeError('start_index must be an integer.')

  if not isinstance(end_index, (int, long)):
    raise TypeError('end_index must be an integer.')

  if isinstance(blob_key, BlobKey):
    blob_key = str(blob_key).decode('utf-8')
  elif isinstance(blob_key, str):
    blob_key = blob_key.decode('utf-8')
  elif not isinstance(blob_key, unicode):
    raise TypeError('blob_key must be str, Unicode or BlobKey: %s' % blob_key)


  if start_index < 0:
    raise DataIndexOutOfRangeError(
        'Cannot fetch blob at negative index.')


  if end_index < start_index:
    raise DataIndexOutOfRangeError(
        'Start index %d > end index %d' % (start_index, end_index))


  fetch_size = end_index - start_index + 1

  if fetch_size > MAX_BLOB_FETCH_SIZE:
    raise BlobFetchSizeTooLargeError(
        'Blob fetch size is too large: %d' % fetch_size)

  request = blobstore_service_pb.FetchDataRequest()
  response = blobstore_service_pb.FetchDataResponse()

  request.set_blob_key(blob_key)
  request.set_start_index(start_index)
  request.set_end_index(end_index)

  return _make_async_call(rpc, 'FetchData', request, response,
                          _get_result_hook, lambda rpc: rpc.response.data())