Software

How to Retrieve Object Metadata in OpenStack Swift

API w/ Java

import static java.lang.System.out;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

public class OpenStackSample {

  public static void main(String[] args) {
    Client client = ClientBuilder.newClient();

    Response res = client.target("https://host/v1/sampleAccount/sampleBucket/sampleObject").request().header("X-Auth-Token", ...).head();

    if (res.getStatus() == 200)
      res.getHeaders().forEach((k, v) -> out.println(k + " = " + v));

    client.close();
  }

}

API w/ Go

package openstacksample

import "gopkg.in/resty.v1"

import "fmt"

func main() {
  res, _ := resty.R().SetHeader("X-Auth-Token", ...).Head("https://host/v1/sampleAccount/sampleBucket/sampleObject")

  if res.StatusCode() == 200 {

    for k, v := range res.Header() {
      fmt.Printf("%v = %v\n", k, v)
    }

  }

}

API w/ Node.js

var https = require('https');

var req = https.request({ headers: { 'x-auth-token': ... }, hostname: 'host', method: 'HEAD', path: '/v1/sampleAccount/sampleBucket/sampleObject' }, (res) => {

  if (res.statusCode === 200)
 
    for (k in res.headers)
      console.log(k + ' = ' + res.headers[k]);

});

req.end();

API w/ Python

import requests

res = requests.head('https://host/v1/sampleAccount/sampleBucket/sampleObject', headers = { 'X-Auth-Token': ... })

if res.ok:
 
  for k, v in res.headers.items():
    print k + ' = ' + v  + '\n'

API w/ Ruby

require 'rest-client'

res = RestClient.head 'https://host/v1/sampleAccount/sampleBucket/sampleObject', :x_auth_token => ...

if res.code == 200
  res.headers.each {|k, v| puts "#{k} = #{v}" }
end

CLI

1) Configure the CLI:

export ST_AUTH=https://host/auth/v1.0
export ST_USER=sampleuser
export ST_KEY=samplepassword

2) Run the following command:

swift stat samplebucket sampleobject

Client

from swiftclient.service import SwiftService

service = SwiftService({ 'auth': 'https://host/auth/v1.0', 'user': 'sampleUser', 'key': 'samplePassword' })

for res in service.stat('sampleBucket', [ 'sampleObject' ]):
  print res

Terraform Provider

N/A.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.