Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Objects: insert

Stores new data blobs and associated metadata. Try it now or see an example .

This method supports an /upload URI and accepts uploaded media with the following characteristics:

  • Maximum file size: 5 TB
  • Accepted Media MIME types: */*

The metadata-only method is suitable only for small objects, as the limit for inline data is 64KB. For uploading larger objects, please see How to Upload Objects .

The authenticated user must have WRITER permissions on the bucket .


Request

HTTP request

This method provides media upload functionality through two separate URIs. For more details, see the document on media upload .

  • Upload URI, for media upload requests:
    POST https://www.googleapis.com/upload/storage/v1beta1/b/bucket/o
  • Metadata URI, for metadata-only requests:
    POST https://www.googleapis.com/storage/v1beta1/b/bucket/o

Parameters

Parameter name Value Description
Path parameters
bucket string Name of the bucket in which to store the new object. Overrides the provided object metadata's bucket value, if any.
Required query parameters
uploadType string The type of upload request to the /upload URI. Acceptable values are:
  • media - Simple upload . Upload the media only, without any metadata.
  • multipart - Multipart upload . Upload both the media and its metadata, in a single request.
  • resumable - Resumable upload . Upload the file in a resumable fashion, using a series of at least two requests where the first request includes the metadata.
Optional query parameters
name string Name of the object. Required when the object metadata is not otherwise provided. Overrides the object metadata's name value, if any.
projection string Set of properties to return. Defaults to no_acl , unless the object resource specifies the acl property, when it defaults to full .

Acceptable values are:
  • " full ": Include all properties.
  • " no_acl ": Omit the acl property.

Request body

In the request body, supply an object resource with the following properties as the metadata. For more information, see the document on media upload .

Property name Value Description Notes
Required Properties
media. contentType string Content-Type of the object data. writable
media. data bytes URL-safe Base64-encoded data. This property can be used to insert objects under 64KB in size, and will only be returned in response to the get method for objects so created. When this resource is returned in response to the list method, this property is omitted.
Optional Properties
acl[] list Access controls on the object. writable
cacheControl string Cache-Control directive for the object data. writable
contentDisposition string Content-Disposition of the object data. writable
contentEncoding string Content-Encoding of the object data. writable
contentLanguage string Content-Language of the object data. writable
media. algorithm string Hash algorithm used. Currently only MD5 is supported. Required if a hash is provided. writable
media. hash string Hash of the data. Required if a hash algorithm is provided. writable
metadata object User-provided metadata, in key/value pairs. writable
metadata. (key) string An individual metadata entry. writable
name string The name of this object. Required if not specified by URL parameter. writable

Response

If successful, this method returns an object resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library .

// Given
InputStream inputStream;  // object data, e.g., FileInputStream
long byteCount;  // size of input stream

InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", inputStream);
// Knowing the stream length is currently required. See
// http://code.google.com/p/google-api-java-client/issues/detail?id=591
mediaContent.setLength(byteCount);

StorageObject objectMetadata = null;

if (useCustomMetadata) {
  // If you have custom settings for metadata on the object you want to set
  // then you can allocate a StorageObject and set the values here. You can
  // leave out setBucket(), since the bucket is in the insert command's
  // parameters.
  objectMetadata = new StorageObject()
      .setName("myobject")
      .setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"))
      .setAcl(ImmutableList.of(
          new ObjectAccessControl().setEntity("domain-example.com").setRole("READER"),
          new ObjectAccessControl().setEntity("[email protected]").setRole("OWNER")
          ))
      .setContentDisposition("attachment");
}

Storage.Objects.Insert insertObject = storage.objects().insert("mybucket", objectMetadata,
    mediaContent);

if (!useCustomMetadata) {
  // If you don't provide metadata, you will have specify the object
  // name by parameter. You will probably also want to ensure that your
  // default object ACLs (a bucket property) are set appropriately:
  // https://developers.google.com/storage/docs/json_api/v1/buckets#defaultObjectAcl
  insertObject.setName("myobject");
}

// For small files, you may wish to call setDirectUploadEnabled(true), to
// reduce the number of HTTP requests made to the server.
if (mediaContent.getLength() > 0 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
  // NOTE: This does not currently retry on retryable failures.
  // http://code.google.com/p/google-api-java-client/issues/detail?id=581
  insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
}

insertObject.execute();

Ruby

Uses the Ruby client library .

# Insert a small object into a bucket using metadata.
# This method only works for objects up to 64 KB.
object_content = 'Insert content here.'
metadata_insert_result = client.execute(
  api_method: storage.objects.insert,
  parameters: {
    uploadType: 'media', 
    bucket: BUCKET, 
    name: OBJECT
  },
  body_object: {
    contentType: 'text/plain', 
    data: Base64.encode64(object_content)
  }
)
contents = metadata_insert_result.data
puts "Metadata insert: #{contents.name} at #{contents.selfLink}" # There are three "normal" (i.e., not metadata) upload types. # "multipart" and "resumable" appear below, but at the time # of writing, the "media" option is not available in the # Ruby API client. # Multipart upload. # This is not resumable, so retry logic (not shown here) # should be handled by the user. media = Google::APIClient::UploadIO.new(FILENAME, MIME_TYPE) multipart_insert_result = client.execute( api_method: storage.objects.insert, parameters: { uploadType: 'multipart', bucket: BUCKET, name: OBJECT }, body_object: {contentType: MIME_TYPE}, media: media ) contents = multipart_insert_result.data
puts "Multipart insert:\n#{contents.name} at #{contents.selfLink}"
# Resumable upload resumable_media = Google::APIClient::UploadIO.new(FILENAME, MIME_TYPE) resumable_result = client.execute( api_method: storage.objects.insert, media: resumable_media, parameters: { uploadType: 'resumable', bucket: BUCKET, name: OBJECT }, body_object: {contentType: MIME_TYPE} ) # Does actual upload of file upload = resumable_result.resumable_upload if upload.resumable? client.execute(upload) end puts "\nResumable insert: "
puts "Created object #{upload.parameters['name']}"



Try it!

Note: APIs Explorer currently supports metadata requests only.

Use the APIs Explorer below to call this method on live data and see the response. Alternatively, try the standalone Explorer .

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.