Software

How to List Objects 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.MediaType;
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").request(MediaType.WILDCARD_TYPE).header("X-Auth-Token", ...).get();

    if (res.getStatus() == 200)
      out.println(res.readEntity(String.class));

    client.close();
  }

}

API w/ Go

package main

import "gopkg.in/resty.v1"

import "fmt"

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

  if res.StatusCode() == 200 {
    fmt.Printf("%v\n", res)
  }

}

API w/ Node.js

var https = require('https');

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

  if (res.statusCode === 200) {
    var body = '';
 
    res.on('data', (chunk) => {
      body += chunk.toString();
    });
 
    res.on('end', () => {
      console.log(body);
    });
 
  }
 
});
 
req.end();

API w/ Python

import requests

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

if res.ok:
  print res.text

API w/ Ruby

require 'rest-client'

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

if res.code == 200
  puts res.body
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 list samplebucket

Client

from swiftclient.service import SwiftService

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

for res in service.list('sampleBucket'):
  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.