Retrieve metadata of an object in a Cloud Storage bucket.
Explore further
For detailed documentation that includes this code sample, see the following:
- Enable and use object retention configurations
- Use customer-managed encryption keys
- Use file metadata with Cloud Storage on Web
- View and edit object metadata
Code sample
C++
For more information, see the Cloud Storage C++ API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
namespace
gcs
=
::
google
::
cloud
::
storage
;
using
::
google
::
cloud
::
StatusOr
;
[](
gcs
::
Client
client
,
std
::
string
const
&
bucket_name
,
std
::
string
const
&
object_name
)
{
StatusOr<gcs
::
ObjectMetadata
>
object_metadata
=
client
.
GetObjectMetadata
(
bucket_name
,
object_name
);
if
(
!
object_metadata
)
throw
std
::
move
(
object_metadata
).
status
();
std
::
cout
<<
"The metadata for object "
<<
object_metadata
-
> name
()
<<
" in bucket "
<<
object_metadata
-
> bucket
()
<<
" is "
<<
*
object_metadata
<<
"
\n
"
;
}
C#
For more information, see the Cloud Storage C# API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
using
Google.Cloud.Storage.V1
;
using
System
;
public
class
GetMetadataSample
{
public
Google
.
Apis
.
Storage
.
v1
.
Data
.
Object
GetMetadata
(
string
bucketName
=
"your-unique-bucket-name"
,
string
objectName
=
"your-object-name"
)
{
var
storage
=
StorageClient
.
Create
();
var
storageObject
=
storage
.
GetObject
(
bucketName
,
objectName
,
new
GetObjectOptions
{
Projection
=
Projection
.
Full
});
Console
.
WriteLine
(
$"Bucket:\t{storageObject.Bucket}"
);
Console
.
WriteLine
(
$"CacheControl:\t{storageObject. CacheControl
}"
);
Console
.
WriteLine
(
$"ComponentCount:\t{storageObject.ComponentCount}"
);
Console
.
WriteLine
(
$"ContentDisposition:\t{storageObject. ContentDisposition
}"
);
Console
.
WriteLine
(
$"ContentEncoding:\t{storageObject. ContentEncoding
}"
);
Console
.
WriteLine
(
$"ContentLanguage:\t{storageObject.ContentLanguage}"
);
Console
.
WriteLine
(
$"ContentType:\t{storageObject. ContentType
}"
);
Console
.
WriteLine
(
$"Crc32c:\t{storageObject.Crc32c}"
);
Console
.
WriteLine
(
$"ETag:\t{storageObject.ETag}"
);
Console
.
WriteLine
(
$"Generation:\t{storageObject.Generation}"
);
Console
.
WriteLine
(
$"Id:\t{storageObject.Id}"
);
Console
.
WriteLine
(
$"Kind:\t{storageObject.Kind}"
);
Console
.
WriteLine
(
$"KmsKeyName:\t{storageObject.KmsKeyName}"
);
Console
.
WriteLine
(
$"Md5Hash:\t{storageObject.Md5Hash}"
);
Console
.
WriteLine
(
$"MediaLink:\t{storageObject.MediaLink}"
);
Console
.
WriteLine
(
$"Metageneration:\t{storageObject.Metageneration}"
);
Console
.
WriteLine
(
$"Name:\t{storageObject.Name}"
);
Console
.
WriteLine
(
$"Retention:\t{storageObject.Retention}"
);
Console
.
WriteLine
(
$"Size:\t{storageObject.Size}"
);
Console
.
WriteLine
(
$"StorageClass:\t{storageObject.StorageClass}"
);
Console
.
WriteLine
(
$"TimeCreated:\t{storageObject.TimeCreated}"
);
Console
.
WriteLine
(
$"Updated:\t{storageObject.Updated}"
);
bool
eventBasedHold
=
storageObject
.
EventBasedHold
??
false
;
Console
.
WriteLine
(
"Event-based hold enabled? {0}"
,
eventBasedHold
);
bool
temporaryHold
=
storageObject
.
TemporaryHold
??
false
;
Console
.
WriteLine
(
"Temporary hold enabled? {0}"
,
temporaryHold
);
Console
.
WriteLine
(
$"RetentionExpirationTime\t{storageObject.RetentionExpirationTime}"
);
if
(
storageObject
.
Metadata
!=
null
)
{
Console
.
WriteLine
(
"Metadata: "
);
foreach
(
var
metadata
in
storageObject
.
Metadata
)
{
Console
.
WriteLine
(
$"{metadata. Key
}:\t{metadata.Value}"
);
}
}
Console
.
WriteLine
(
$"CustomTime:\t{storageObject.CustomTime}"
);
return
storageObject
;
}
}
Go
For more information, see the Cloud Storage Go API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
import
(
"context"
"fmt"
"io"
"time"
"cloud.google.com/go/storage"
)
// getMetadata prints all of the object attributes.
func
getMetadata
(
w
io
.
Writer
,
bucket
,
object
string
)
(
*
storage
.
ObjectAttrs
,
error
)
{
// bucket := "bucket-name"
// object := "object-name"
ctx
:=
context
.
Background
()
client
,
err
:=
storage
.
NewClient
(
ctx
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"storage.NewClient: %w"
,
err
)
}
defer
client
.
Close
()
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
10
)
defer
cancel
()
o
:=
client
.
Bucket
(
bucket
).
Object
(
object
)
attrs
,
err
:=
o
.
Attrs
(
ctx
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Object(%q).Attrs: %w"
,
object
,
err
)
}
fmt
.
Fprintf
(
w
,
"Bucket: %v\n"
,
attrs
.
Bucket
)
fmt
.
Fprintf
(
w
,
"CacheControl: %v\n"
,
attrs
.
CacheControl
)
fmt
.
Fprintf
(
w
,
"ContentDisposition: %v\n"
,
attrs
.
ContentDisposition
)
fmt
.
Fprintf
(
w
,
"ContentEncoding: %v\n"
,
attrs
.
ContentEncoding
)
fmt
.
Fprintf
(
w
,
"ContentLanguage: %v\n"
,
attrs
.
ContentLanguage
)
fmt
.
Fprintf
(
w
,
"ContentType: %v\n"
,
attrs
.
ContentType
)
fmt
.
Fprintf
(
w
,
"Crc32c: %v\n"
,
attrs
.
CRC32C
)
fmt
.
Fprintf
(
w
,
"Generation: %v\n"
,
attrs
.
Generation
)
fmt
.
Fprintf
(
w
,
"KmsKeyName: %v\n"
,
attrs
.
KMSKeyName
)
fmt
.
Fprintf
(
w
,
"Md5Hash: %v\n"
,
attrs
.
MD5
)
fmt
.
Fprintf
(
w
,
"MediaLink: %v\n"
,
attrs
.
MediaLink
)
fmt
.
Fprintf
(
w
,
"Metageneration: %v\n"
,
attrs
.
Metageneration
)
fmt
.
Fprintf
(
w
,
"Name: %v\n"
,
attrs
.
Name
)
fmt
.
Fprintf
(
w
,
"Size: %v\n"
,
attrs
.
Size
)
fmt
.
Fprintf
(
w
,
"StorageClass: %v\n"
,
attrs
.
StorageClass
)
fmt
.
Fprintf
(
w
,
"TimeCreated: %v\n"
,
attrs
.
Created
)
fmt
.
Fprintf
(
w
,
"Updated: %v\n"
,
attrs
.
Updated
)
fmt
.
Fprintf
(
w
,
"Event-based hold enabled? %t\n"
,
attrs
.
EventBasedHold
)
fmt
.
Fprintf
(
w
,
"Temporary hold enabled? %t\n"
,
attrs
.
TemporaryHold
)
fmt
.
Fprintf
(
w
,
"Retention expiration time %v\n"
,
attrs
.
RetentionExpirationTime
)
fmt
.
Fprintf
(
w
,
"Custom time %v\n"
,
attrs
.
CustomTime
)
fmt
.
Fprintf
(
w
,
"Retention: %+v\n"
,
attrs
.
Retention
)
fmt
.
Fprintf
(
w
,
"\n\nMetadata\n"
)
for
key
,
value
:=
range
attrs
.
Metadata
{
fmt
.
Fprintf
(
w
,
"\t%v = %v\n"
,
key
,
value
)
}
return
attrs
,
nil
}
Java
For more information, see the Cloud Storage Java API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
import
com.google.cloud.storage. Blob
;
import
com.google.cloud.storage. Storage
;
import
com.google.cloud.storage. StorageException
;
import
com.google.cloud.storage. StorageOptions
;
import
java.util.Date
;
import
java.util.Map
;
public
class
GetObjectMetadata
{
public
static
void
getObjectMetadata
(
String
projectId
,
String
bucketName
,
String
blobName
)
throws
StorageException
{
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The ID of your GCS object
// String objectName = "your-object-name";
Storage
storage
=
StorageOptions
.
newBuilder
().
setProjectId
(
projectId
).
build
().
getService
();
// Select all fields
// Fields can be selected individually e.g. Storage.BlobField.CACHE_CONTROL
Blob
blob
=
storage
.
get
(
bucketName
,
blobName
,
Storage
.
BlobGetOption
.
fields
(
Storage
.
BlobField
.
values
()));
// Print blob metadata
System
.
out
.
println
(
"Bucket: "
+
blob
.
getBucket
());
System
.
out
.
println
(
"CacheControl: "
+
blob
.
getCacheControl
());
System
.
out
.
println
(
"ComponentCount: "
+
blob
.
getComponentCount
());
System
.
out
.
println
(
"ContentDisposition: "
+
blob
.
getContentDisposition
());
System
.
out
.
println
(
"ContentEncoding: "
+
blob
.
getContentEncoding
());
System
.
out
.
println
(
"ContentLanguage: "
+
blob
.
getContentLanguage
());
System
.
out
.
println
(
"ContentType: "
+
blob
.
getContentType
());
System
.
out
.
println
(
"CustomTime: "
+
blob
.
getCustomTime
());
System
.
out
.
println
(
"Crc32c: "
+
blob
.
getCrc32c
());
System
.
out
.
println
(
"Crc32cHexString: "
+
blob
.
getCrc32cToHexString
());
System
.
out
.
println
(
"ETag: "
+
blob
.
getEtag
());
System
.
out
.
println
(
"Generation: "
+
blob
.
getGeneration
());
System
.
out
.
println
(
"Id: "
+
blob
.
getBlobId
());
System
.
out
.
println
(
"KmsKeyName: "
+
blob
.
getKmsKeyName
());
System
.
out
.
println
(
"Md5Hash: "
+
blob
.
getMd5
());
System
.
out
.
println
(
"Md5HexString: "
+
blob
.
getMd5ToHexString
());
System
.
out
.
println
(
"MediaLink: "
+
blob
.
getMediaLink
());
System
.
out
.
println
(
"Metageneration: "
+
blob
.
getMetageneration
());
System
.
out
.
println
(
"Name: "
+
blob
.
getName
());
System
.
out
.
println
(
"Size: "
+
blob
.
getSize
());
System
.
out
.
println
(
"StorageClass: "
+
blob
.
getStorageClass
());
System
.
out
.
println
(
"TimeCreated: "
+
new
Date
(
blob
.
getCreateTime
()));
System
.
out
.
println
(
"Last Metadata Update: "
+
new
Date
(
blob
.
getUpdateTime
()));
System
.
out
.
println
(
"Object Retention Policy: "
+
blob
.
getRetention
());
Boolean
temporaryHoldIsEnabled
=
(
blob
.
getTemporaryHold
()
!=
null
&&
blob
.
getTemporaryHold
());
System
.
out
.
println
(
"temporaryHold: "
+
(
temporaryHoldIsEnabled
?
"enabled"
:
"disabled"
));
Boolean
eventBasedHoldIsEnabled
=
(
blob
.
getEventBasedHold
()
!=
null
&&
blob
.
getEventBasedHold
());
System
.
out
.
println
(
"eventBasedHold: "
+
(
eventBasedHoldIsEnabled
?
"enabled"
:
"disabled"
));
if
(
blob
.
getRetentionExpirationTime
()
!=
null
)
{
System
.
out
.
println
(
"retentionExpirationTime: "
+
new
Date
(
blob
.
getRetentionExpirationTime
()));
}
if
(
blob
.
getMetadata
()
!=
null
)
{
System
.
out
.
println
(
"\n\n\nUser metadata:"
);
for
(
Map
.
Entry<String
,
String
>
userMetadata
:
blob
.
getMetadata
().
entrySet
())
{
System
.
out
.
println
(
userMetadata
.
getKey
()
+
"="
+
userMetadata
.
getValue
());
}
}
}
}
Node.js
For more information, see the Cloud Storage Node.js API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';
// The ID of your GCS file
// const fileName = 'your-file-name';
// Imports the Google Cloud client library
const
{
Storage
}
=
require
(
' @google-cloud/storage
'
);
// Creates a client
const
storage
=
new
Storage
();
async
function
getMetadata
()
{
// Gets the metadata for the file
const
[
metadata
]
=
await
storage
.
bucket
(
bucketName
)
.
file
(
fileName
)
.
getMetadata
();
console
.
log
(
`Bucket:
${
metadata
.
bucket
}
`
);
console
.
log
(
`CacheControl:
${
metadata
.
cacheControl
}
`
);
console
.
log
(
`ComponentCount:
${
metadata
.
componentCount
}
`
);
console
.
log
(
`ContentDisposition:
${
metadata
.
contentDisposition
}
`
);
console
.
log
(
`ContentEncoding:
${
metadata
.
contentEncoding
}
`
);
console
.
log
(
`ContentLanguage:
${
metadata
.
contentLanguage
}
`
);
console
.
log
(
`ContentType:
${
metadata
.
contentType
}
`
);
console
.
log
(
`CustomTime:
${
metadata
.
customTime
}
`
);
console
.
log
(
`Crc32c:
${
metadata
.
crc32c
}
`
);
console
.
log
(
`ETag:
${
metadata
.
etag
}
`
);
console
.
log
(
`Generation:
${
metadata
.
generation
}
`
);
console
.
log
(
`Id:
${
metadata
.
id
}
`
);
console
.
log
(
`KmsKeyName:
${
metadata
.
kmsKeyName
}
`
);
console
.
log
(
`Md5Hash:
${
metadata
.
md5Hash
}
`
);
console
.
log
(
`MediaLink:
${
metadata
.
mediaLink
}
`
);
console
.
log
(
`Metageneration:
${
metadata
.
metageneration
}
`
);
console
.
log
(
`Name:
${
metadata
.
name
}
`
);
console
.
log
(
`Size:
${
metadata
.
size
}
`
);
console
.
log
(
`StorageClass:
${
metadata
.
storageClass
}
`
);
console
.
log
(
`TimeCreated:
${
new
Date
(
metadata
.
timeCreated
)
}
`
);
console
.
log
(
`Last Metadata Update:
${
new
Date
(
metadata
.
updated
)
}
`
);
console
.
log
(
`TurboReplication:
${
metadata
.
rpo
}
`
);
console
.
log
(
`temporaryHold:
${
metadata
.
temporaryHold
?
'enabled'
:
'disabled'
}
`
);
console
.
log
(
`eventBasedHold:
${
metadata
.
eventBasedHold
?
'enabled'
:
'disabled'
}
`
);
if
(
metadata
.
retentionExpirationTime
)
{
console
.
log
(
`retentionExpirationTime:
${
new
Date
(
metadata
.
retentionExpirationTime
)
}
`
);
}
if
(
metadata
.
metadata
)
{
console
.
log
(
'\n\n\nUser metadata:'
);
for
(
const
key
in
metadata
.
metadata
)
{
console
.
log
(
`
${
key
}
=
${
metadata
.
metadata
[
key
]
}
`
);
}
}
}
getMetadata
().
catch
(
console
.
error
);
PHP
For more information, see the Cloud Storage PHP API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
use Google\Cloud\Storage\StorageClient;
/**
* List object metadata.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $objectName The name of your Cloud Storage object.
* (e.g. 'my-object')
*/
function object_metadata(string $bucketName, string $objectName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$info = $object->info();
if (isset($info['name'])) {
printf('Blob: %s' . PHP_EOL, $info['name']);
}
if (isset($info['bucket'])) {
printf('Bucket: %s' . PHP_EOL, $info['bucket']);
}
if (isset($info['storageClass'])) {
printf('Storage class: %s' . PHP_EOL, $info['storageClass']);
}
if (isset($info['id'])) {
printf('ID: %s' . PHP_EOL, $info['id']);
}
if (isset($info['size'])) {
printf('Size: %s' . PHP_EOL, $info['size']);
}
if (isset($info['updated'])) {
printf('Updated: %s' . PHP_EOL, $info['updated']);
}
if (isset($info['generation'])) {
printf('Generation: %s' . PHP_EOL, $info['generation']);
}
if (isset($info['metageneration'])) {
printf('Metageneration: %s' . PHP_EOL, $info['metageneration']);
}
if (isset($info['etag'])) {
printf('Etag: %s' . PHP_EOL, $info['etag']);
}
if (isset($info['crc32c'])) {
printf('Crc32c: %s' . PHP_EOL, $info['crc32c']);
}
if (isset($info['md5Hash'])) {
printf('MD5 Hash: %s' . PHP_EOL, $info['md5Hash']);
}
if (isset($info['contentType'])) {
printf('Content-type: %s' . PHP_EOL, $info['contentType']);
}
if (isset($info['temporaryHold'])) {
printf('Temporary hold: %s' . PHP_EOL, ($info['temporaryHold'] ? 'enabled' : 'disabled'));
}
if (isset($info['eventBasedHold'])) {
printf('Event-based hold: %s' . PHP_EOL, ($info['eventBasedHold'] ? 'enabled' : 'disabled'));
}
if (isset($info['retentionExpirationTime'])) {
printf('Retention Expiration Time: %s' . PHP_EOL, $info['retentionExpirationTime']);
}
if (isset($info['retention'])) {
printf('Retention mode: %s' . PHP_EOL, $info['retention']['mode']);
printf('Retain until time is: %s' . PHP_EOL, $info['retention']['retainUntilTime']);
}
if (isset($info['customTime'])) {
printf('Custom Time: %s' . PHP_EOL, $info['customTime']);
}
if (isset($info['metadata'])) {
printf('Metadata: %s' . PHP_EOL, print_r($info['metadata'], true));
}
}
Python
For more information, see the Cloud Storage Python API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
from
google.cloud
import
storage
def
blob_metadata
(
bucket_name
,
blob_name
):
"""Prints out a blob's metadata."""
# bucket_name = 'your-bucket-name'
# blob_name = 'your-object-name'
storage_client
=
storage
.
Client
()
bucket
=
storage_client
.
bucket
(
bucket_name
)
# Retrieve a blob, and its metadata, from Google Cloud Storage.
# Note that `get_blob` differs from `Bucket.blob`, which does not
# make an HTTP request.
blob
=
bucket
.
get_blob
(
blob_name
)
print
(
f
"Blob:
{
blob
.
name
}
"
)
print
(
f
"Bucket:
{
blob
.
bucket
.
name
}
"
)
print
(
f
"Storage class:
{
blob
.
storage_class
}
"
)
print
(
f
"ID:
{
blob
.
id
}
"
)
print
(
f
"Size:
{
blob
.
size
}
bytes"
)
print
(
f
"Updated:
{
blob
.
updated
}
"
)
print
(
f
"Generation:
{
blob
.
generation
}
"
)
print
(
f
"Metageneration:
{
blob
.
metageneration
}
"
)
print
(
f
"Etag:
{
blob
.
etag
}
"
)
print
(
f
"Owner:
{
blob
.
owner
}
"
)
print
(
f
"Component count:
{
blob
.
component_count
}
"
)
print
(
f
"Crc32c:
{
blob
.
crc32c
}
"
)
print
(
f
"md5_hash:
{
blob
.
md5_hash
}
"
)
print
(
f
"Cache-control:
{
blob
.
cache_control
}
"
)
print
(
f
"Content-type:
{
blob
.
content_type
}
"
)
print
(
f
"Content-disposition:
{
blob
.
content_disposition
}
"
)
print
(
f
"Content-encoding:
{
blob
.
content_encoding
}
"
)
print
(
f
"Content-language:
{
blob
.
content_language
}
"
)
print
(
f
"Metadata:
{
blob
.
metadata
}
"
)
print
(
f
"Medialink:
{
blob
.
media_link
}
"
)
print
(
f
"Custom Time:
{
blob
.
custom_time
}
"
)
print
(
"Temporary hold: "
,
"enabled"
if
blob
.
temporary_hold
else
"disabled"
)
print
(
"Event based hold: "
,
"enabled"
if
blob
.
event_based_hold
else
"disabled"
,
)
print
(
f
"Retention mode:
{
blob
.
retention
.
mode
}
"
)
print
(
f
"Retention retain until time:
{
blob
.
retention
.
retain_until_time
}
"
)
if
blob
.
retention_expiration_time
:
print
(
f
"retentionExpirationTime:
{
blob
.
retention_expiration_time
}
"
)
Ruby
For more information, see the Cloud Storage Ruby API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
def
get_metadata
bucket_name
:,
file_name
:
# The ID of your GCS bucket
# bucket_name = "your-unique-bucket-name"
# The ID of your GCS object
# file_name = "your-file-name"
require
"google/cloud/storage"
storage
=
Google
::
Cloud
::
Storage
.
new
bucket
=
storage
.
bucket
bucket_name
file
=
bucket
.
file
file_name
puts
"Name:
#{
file
.
name
}
"
puts
"Bucket:
#{
bucket
.
name
}
"
puts
"Storage class:
#{
bucket
.
storage_class
}
"
puts
"ID:
#{
file
.
id
}
"
puts
"Size:
#{
file
.
size
}
bytes"
puts
"Created:
#{
file
.
created_at
}
"
puts
"Updated:
#{
file
.
updated_at
}
"
puts
"Generation:
#{
file
.
generation
}
"
puts
"Metageneration:
#{
file
.
metageneration
}
"
puts
"Etag:
#{
file
.
etag
}
"
puts
"Owners:
#{
file
.
acl
.
owners
.
join
','
}
"
puts
"Crc32c:
#{
file
.
crc32c
}
"
puts
"md5_hash:
#{
file
.
md5
}
"
puts
"Cache-control:
#{
file
.
cache_control
}
"
puts
"Content-type:
#{
file
.
content_type
}
"
puts
"Content-disposition:
#{
file
.
content_disposition
}
"
puts
"Content-encoding:
#{
file
.
content_encoding
}
"
puts
"Content-language:
#{
file
.
content_language
}
"
puts
"KmsKeyName:
#{
file
.
kms_key
}
"
puts
"Event-based hold enabled?:
#{
file
.
event_based_hold?
}
"
puts
"Temporary hold enaled?:
#{
file
.
temporary_hold?
}
"
puts
"Retention Expiration:
#{
file
.
retention_expires_at
}
"
puts
"Custom Time:
#{
file
.
custom_time
}
"
puts
"Metadata:"
file
.
metadata
.
each
do
|
key
,
value
|
puts
" -
#{
key
}
=
#{
value
}
"
end
end
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .