Software

How to Create/Update an Object in OpenStack Swift

API w/ Java

W/o Metadata

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

public class OpenStackSample {

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

    client.target("https://host/v1/sampleAccount/sampleBucket/sampleObject").request().header("X-Auth-Token", ...).put(sampleFile);

    client.close();
  }

}

W/ Metadata

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

public class OpenStackSample {

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

    client.target("https://host/v1/sampleAccount/sampleBucket/sampleObject").request().header("X-Auth-Token", ...).header("X-Object-Meta-Sample-Key", "Sample Value").put(sampleFile);

    client.close();
  }

}

API w/ Go

W/o Metadata

package main

import "gopkg.in/resty.v1"

func main() {
  resty.R().SetHeader("X-Auth-Token", ...).SetBody(sampleFile).Put("https://host/v1/sampleAccount/sampleBucket/sampleObject")
}

W/ Metadata

package main

import "gopkg.in/resty.v1"

func main() {
  resty.R().SetHeader("X-Auth-Token", ...).SetHeader("X-Object-Meta-Sample-Key", "Sample Value").SetBody(sampleFile).Put("https://host/v1/sampleAccount/sampleBucket/sampleObject")
}

API w/ Node.js

W/o Metadata

var https = require('https');

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

req.write(sampleFile);

req.end();

W/ Metadata

var https = require('https');

var req = https.request({ headers: { 'x-auth-token': ..., 'x-object-meta-sample-key': 'Sample Value' }, hostname: 'host', method: 'PUT', path: '/v1/sampleAccount/sampleBucket/sampleObject' });

req.write(sampleFile);

req.end();

API w/ Python

W/o Metadata

import requests

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

W/ Metadata

import requests

requests.put('https://host/v1/sampleAccount/sampleBucket/sampleObject', headers = { 'X-Auth-Token': ..., 'X-Object-Meta-Sample-Key': 'Sample Value' }, data = sampleFile)

API w/ Ruby

W/o Metadata

require 'rest-client'

RestClient.put 'https://host/v1/sampleAccount/sampleBucket/sampleObject', sampleFile, :x_auth_token => ...

W/o Metadata

require 'rest-client'

RestClient.put 'https://host/v1/sampleAccount/sampleBucket/sampleObject', sampleFile, :x_auth_token => ..., :x_container_meta_sample_key => 'Sample Value'

CLI

W/o Metadata

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 upload samplebucket samplefile --object-name sampleobject

W/ Metadata

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 upload samplebucket samplefile --object-name sampleobject -H "X-Object-Meta-Sample-Key:Sample Value"

Client

W/o Metadata

from swiftclient.service import SwiftService, SwiftUploadObject

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

for res in service.upload('sampleBucket', [ SwiftUploadObject(sampleFile, 'sampleObject') ]):
  print res

W/ Metadata

from swiftclient.service import SwiftService, SwiftUploadObject

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

for res in service.upload('sampleBucket', [ SwiftUploadObject(sampleFile, 'sampleObject') ], options = { 'header': [ 'X-Object-Meta-Sample-Key:Sample Value' ] } ):
  print res

Terraform Provider

W/o Metadata

1) Configure the provider:

export OS_SWAUTH=true
export OS_AUTH_URL=https://host/
export OS_USERNAME=sampleuser
export OS_PASSWORD=samplepassword

2) Create the configuration:

resource "openstack_objectstorage_object_v1" "sampleobject" {
  name = "sampleobject"
  container_name = "samplebucket"
  content = samplefile
}

3) Apply the change.

W/ Metadata

1) Configure the provider:

export OS_SWAUTH=true
export OS_AUTH_URL=https://host
export OS_USERNAME=sampleuser
export OS_PASSWORD=samplepassword/

2) Create the configuration:

resource "openstack_objectstorage_object_v1" "sampleobject" {
  name = "sampleobject"
  container_name = "samplebucket"
  content = samplefile
  metadata {
    Sample-Key = "Sample Value"
  }
}

3) Apply the change.

Software

How to Delete Bucket Metadata in OpenStack Swift

API w/ Java

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;

public class OpenStackSample {

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

    client.target("https://host/v1/sampleAccount/sampleBucket").request().header("X-Auth-Token",  ...).header("X-Remove-Container-Meta-Sample-Key", "").post(Entity.entity("", MediaType.WILDCARD_TYPE));

    client.close();
  }

}

API w/ Go

package main

import "gopkg.in/resty.v1"

func main() {
  resty.R().SetHeader("X-Auth-Token", ...).SetHeader("X-Remove-Container-Meta-Sample-Key", "").Post("https://host/v1/sampleAccount/sampleBucket")
}

API w/ Node.js

var https = require('https');

var req = https.request({ headers: { 'x-auth-token': ..., 'x-remove-container-meta-sample-key': '' }, hostname: 'host', method: 'POST', path: '/v1/sampleAccount/sampleBucket' });

req.end();

API w/ Python

import requests

requests.post('https://host/v1/sampleAccount/sampleBucket', headers = { 'X-Auth-Token': ..., 'X-Remove-Container-Meta-Sample-Key': '' })

API w/ Ruby

require 'rest-client'

RestClient.post 'https://host/v1/sampleAccount/sampleBucket', '', :x_auth_token => ..., :x_remove_container_meta_sample_key => ''

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 post samplebucket -H "X-Remove-Container-Meta-Sample-Key:"

Client

from swiftclient.service import SwiftService

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

for res in service.post('samplebucket', options = { 'header': [ 'X-Remove-Container-Meta-Sample-Key:' ] }):
  print res

Terraform Provider

1) Configure the provider:

export OS_SWAUTH=true
export OS_AUTH_URL=https://host/
export OS_USERNAME=sampleuser
export OS_PASSWORD=samplepassword

2) Remove the metadata values from the bucket configuration.

3) Apply the change.

Software

How to Retrieve Bucket 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.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", ...).head();

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

    client.close();
  }

}

API w/ Go

package main

import "gopkg.in/resty.v1"

import "fmt"

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

  if res.StatusCode() == 204 {

    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'}, (res) => {

  if (res.statusCode === 204)

    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', 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', :x_auth_token => ...

if res.code == 204
  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

Client

from swiftclient.service import SwiftService

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

for res in service.stat('samplebucket'):
  print res

Terraform Provider

N/A.

Software

How to Create/Update Bucket Metadata in OpenStack Swift

API w/ Java

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;

public class OpenStackSample {

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

    client.target("https://host/v1/sampleAccount/sampleBucket").request().header("X-Auth-Token",  ...).header("X-Container-Meta-Sample-Key", "Sample Value").post(Entity.entity("", MediaType.WILDCARD_TYPE));

    client.close();
  }

}

API w/ Go

package main

import "gopkg.in/resty.v1"

func main() {
  resty.R().SetHeader("X-Auth-Token", ...).SetHeader("X-Container-Meta-Sample-Key", "Sample Value").Post("https://host/v1/sampleAccount/sampleBucket")
}

API w/ Node.js

var https = require('https');

var req = https.request({ headers: { 'x-auth-token': ..., 'x-container-meta-sample-key': 'Sample Value' }, hostname: 'host', method: 'POST', path: '/v1/sampleAccount/sampleBucket'});

req.end();

API w/ Python

import requests

requests.post('https://host/v1/sampleAccount/sampleBucket', headers = { 'X-Auth-Token': ..., 'X-Container-Meta-Sample-Key': 'Sample Value' })

API w/ Ruby

require 'rest-client'

RestClient.post 'https://host/v1/sampleAccount/sampleBucket', '', :x_auth_token => ..., :x_container_meta_sample_key => 'Sample Value'

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 post samplebucket -m "Sample-Key: Sample Value"

Client

from swiftclient.service import SwiftService

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

for res in service.post('sampleBucket', options = { 'meta': [ 'Sample-Key:Sample Value' ] }):
  print res

Terraform Provider

1) Configure the provider:

export OS_SWAUTH=true
export OS_AUTH_URL=https://host/
export OS_USERNAME=sampleuser
export OS_PASSWORD=samplepassword

2) Create the configuration:

resource "openstack_objectstorage_container_v1" "samplebucket" {
  name = "samplebucket"
  metadata {
    Sample-Key = "Sample Value"
  }
}

3) Apply the change.

Software

How to Delete a Bucket in OpenStack Swift

API w/ Java

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

public class OpenStackSample {

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

    client.target("https://host/v1/sampleAccount/sampleBucket").request().header("X-Auth-Token", ...).delete();

    client.close();
  }

}

API w/ Go

package main

import "gopkg.in/resty.v1"

func main() {
  resty.R().SetHeader("X-Auth-Token", ...).Delete("https://host/v1/sampleAccount/sampleBucket")
}

API w/ Node.js

var https = require('https');

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

API w/ Python

import requests

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

API w/ Ruby

require 'rest-client'
 
RestClient.delete 'https://host/v1/sampleAccount/sampleBucket', :x_auth_token => ...

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 delete samplebucket

Client

from swiftclient.service import SwiftService

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

for res in service.delete('sampleBucket'):
  print res

Terraform Provider

1) Configure the provider:

export OS_SWAUTH=true
export OS_AUTH_URL=https://host/
export OS_USERNAME=sampleuser
export OS_PASSWORD=samplepassword

2) If needed, import the bucket:

terraform import openstack_objectstorage_container_v1.samplebucket samplebucket

3) Destroy the bucket.

Software

How to List Buckets 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").request().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")

  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'}, (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', headers = { 'X-Auth-Token': ... })

if res.ok:
  print res.text

API w/ Ruby

require 'rest-client'

res = RestClient.get 'https://host/v1/sampleAccount', :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

Client

from swiftclient.service import SwiftService

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

for res in service.list():

  for b in res['listing']:
    print b['name']

Terraform Provider

N/A.

Software

How to Create/Update a Bucket in OpenStack Swift

API w/ Java

W/o Metadata

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;

public class OpenStackSample {

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

    client.target("https://host/v1/sampleAccount/sampleBucket").request().header("X-Auth-Token", ...).put(Entity.entity("", MediaType.WILDCARD_TYPE));

    client.close();
  }

}

W/ Metadata

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;

public class OpenStackSample {

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

    client.target("https://host/v1/sampleAccount/sampleBucket").request().header("X-Auth-Token", ...).header("X-Container-Meta-Sample-Key", "Sample Value").put(Entity.entity("", MediaType.WILDCARD_TYPE));

    client.close();
  }

}

API w/ Go

W/o Metadata

package main

import "gopkg.in/resty.v1"

func main() {
  resty.R().SetHeader("X-Auth-Token", ...).Put("https://host/v1/sampleAccount/sampleBucket")
}

W/ Metadata

package main

import "gopkg.in/resty.v1"

func main() {
  resty.R().SetHeader("X-Auth-Token", ...).SetHeader("X-Container-Meta-Sample-Key", "Sample Value").Put("https://host/v1/sampleAccount/sampleBucket")
}

API w/ Node.js

W/o Metadata

var https = require('https');

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

req.end();

W/ Metadata

var https = require('https');

var req = https.request({ headers: { 'x-auth-token': ..., 'x-content-meta-sample-key': 'Sample Value' }, hostname: 'host', method: 'PUT', path: '/v1/sampleAccount/sampleBucket'});

req.end();

API w/ Python

W/o Metadata

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

W/ Metadata

import requests
 
requests.put('https://host/v1/sampleAccount/sampleBucket', headers = { 'X-Auth-Token': ..., 'X-Container-Meta-Sample-Key': 'Sample Value' })

API w/ Ruby

W/o Metadata

require 'rest-client'
 
RestClient.put 'https://host/v1/sampleAccount/sampleBucket', '', :x_auth_token => ...

W/ Metadata

require 'rest-client'

RestClient.put 'https://host/v1/sampleAccount/sampleBucket', '', :x_auth_token => ..., :x_container_meta_sample_key => 'Sample Value'

CLI

W/o Metadata

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 post samplebucket

W/ Metadata

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 post samplebucket -m "Sample-Key:Sample Value"

Client

W/o Metadata

from swiftclient.service import SwiftService

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

for res in service.post('sampleBucket'):
  print res

W/ Metadata

from swiftclient.service import SwiftService

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

for res in service.post('sampleBucket', options = { 'meta': [ 'Sample-Key:Sample Value' ] }):
  print res

Terraform Provider

W/o Metadata

1) Configure the provider:

export OS_SWAUTH=true
export OS_AUTH_URL=https://host/
export OS_USERNAME=sampleuser
export OS_PASSWORD=samplepassword

2) Create the configuration:

resource "openstack_objectstorage_container_v1" "samplebucket" {
  name = "samplebucket"
}

3) Apply the change.

W/ Metadata

1) Configure the provider:

export OS_SWAUTH=true
export OS_AUTH_URL=https://host/
export OS_USERNAME=sampleuser
export OS_PASSWORD=samplepassword

2) Create the configuration:

resource "openstack_objectstorage_container_v1" "samplebucket" {
  name = "samplebucket"
  metadata {
    Sample-Key = "Sample Value"
  }
}

3) Apply the change.

Software

How to Generate an Authentication Token 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/auth/v1.0").request().header("X-Storage-User", "sampleUser").header("X-Storage-Pass", "samplePassword").get();

    if (res.getStatus() == 200)
      out.println(res.getHeaderString("X-Auth-Token"));

    client.close();
  }

}

API w/ Go

package main

import "gopkg.in/resty.v1"

import "fmt"

func main() {
  res, _ := resty.R().SetHeader("X-Storage-User", "sampleUser").SetHeader("X-Storage-Pass", "samplePassword").Get("https://host/auth/v1.0")

  if res.StatusCode() == 200 {
    fmt.Printf("%v", res.Header().Get("X-Auth-Token"))
  }

}

API w/ Node.js

var https = require('https');

var req = https.request({ headers: { 'x-storage-user': 'sampleUser', 'x-storage-pass': 'samplePassword' }, hostname: 'host', method: 'GET', path: '/auth/v1.0'}, (res) => {

  if (res.statusCode === 200)
    console.log(res.headers['x-auth-token']);

});

req.end();

API w/ Python

import requests

res = requests.get('https://host/auth/v1.0', headers = { 'X-Storage-User': 'sampleUser', 'X-Storage-Pass': 'samplePassword' })

if res.ok:
  print res.headers['X-Auth-Token']

API w/ Ruby

require 'rest-client'

res = RestClient.get 'https://host/auth/v1.0', :x_storage_user => 'sampleUser', :x_storage_pass => 'samplePassword'

if res.code == 200
 puts res.headers[:x_auth_token]
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 auth

Client

from swiftclient import client

auth = client.get_auth('https://host/auth/v1.0', sampleUser, samplePassword)

print auth[1]

Terraform Provider

N/A.

Software

OCI Object Storage Tools