0
点赞
收藏
分享

微信扫一扫

Swift 之 Ceph object Gateway

杏花疏影1 2023-03-14 阅读 36

TUTORIAL

Swift 之 Ceph object Gateway_ceph

RESTful API

参考 ​​https://docs.openstack.org/api-ref/object-store/index.html​​


SWIFT EXAMPLES

JAVA SWIFT EXAMPLES

SETUP​

The following examples may require some or all of the following Java classes to be imported:

import org.javaswift.joss.client.factory.AccountConfig;
import org.javaswift.joss.client.factory.AccountFactory;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;
import org.javaswift.joss.model.StoredObject;
import java.io.File;
import java.io.IOException;
import java.util.*;

CREATE A CONNECTION​

This creates a connection so that you can interact with the server:

String username = "USERNAME";
String password = "PASSWORD";
String authUrl = "https://radosgw.endpoint/auth/1.0";

AccountConfig config = new AccountConfig();
config.setUsername(username);
config.setPassword(password);
config.setAuthUrl(authUrl);
config.setAuthenticationMethod(AuthenticationMethod.BASIC);
Account account = new AccountFactory(config).createAccount();

CREATE A CONTAINER​

This creates a new container called ​​my-new-container​​:

Container container = account.getContainer("my-new-container");
container.create();

CREATE AN OBJECT​

This creates an object ​​foo.txt​​​ from the file named ​​foo.txt​​​ in the container ​​my-new-container​​:

Container container = account.getContainer("my-new-container");
StoredObject object = container.getObject("foo.txt");
object.uploadObject(new File("foo.txt"));

ADD/UPDATE OBJECT METADATA​

This adds the metadata key-value pair ​​key​​​:​​value​​​ to the object named ​​foo.txt​​​ in the container ​​my-new-container​​:

Container container = account.getContainer("my-new-container");
StoredObject object = container.getObject("foo.txt");
Map<String, Object> metadata = new TreeMap<String, Object>();
metadata.put("key", "value");
object.setMetadata(metadata);

LIST OWNED CONTAINERS​

This gets a list of Containers that you own. This also prints out the container name.

Collection<Container> containers = account.list();
for (Container currentContainer : containers) {
System.out.println(currentContainer.getName());
}

The output will look something like this:

mahbuckat1
mahbuckat2
mahbuckat3

LIST A CONTAINER’S CONTENT​

This gets a list of objects in the container ​​my-new-container​​; and, it also prints out each object’s name, the file size, and last modified date:

Container container = account.getContainer("my-new-container");
Collection<StoredObject> objects = container.list();
for (StoredObject currentObject : objects) {
System.out.println(currentObject.getName());
}

The output will look something like this:

myphoto1.jpg
myphoto2.jpg

RETRIEVE AN OBJECT’S METADATA​

This retrieves metadata and gets the MIME type for an object named ​​foo.txt​​​ in a container named ​​my-new-container​​:

Container container = account.getContainer("my-new-container");
StoredObject object = container.getObject("foo.txt");
Map<String, Object> returnedMetadata = object.getMetadata();
for (String name : returnedMetadata.keySet()) {
System.out.println("META / "+name+": "+returnedMetadata.get(name));
}

RETRIEVE AN OBJECT​

This downloads the object ​​foo.txt​​​ in the container ​​my-new-container​​​ and saves it in ​​./outfile.txt​​:

Container container = account.getContainer("my-new-container");
StoredObject object = container.getObject("foo.txt");
object.downloadObject(new File("outfile.txt"));

DELETE AN OBJECT​

This deletes the object ​​goodbye.txt​​ in the container “my-new-container”:

Container container = account.getContainer("my-new-container");
StoredObject object = container.getObject("foo.txt");
object.delete();

DELETE A CONTAINER​

This deletes a container named “my-new-container”:

Container container = account.getContainer("my-new-container");
container.delete();

PYTHON SWIFT EXAMPLES

CREATE A CONNECTION​

This creates a connection so that you can interact with the server:

import swiftclient
user = 'account_name:username'
key = 'your_api_key'

conn = swiftclient.Connection(
user=user,
key=key,
authurl='https://objects.dreamhost.com/auth',
)

CREATE A CONTAINER​

This creates a new container called ​​my-new-container​​:

container_name = 'my-new-container'
conn.put_container(container_name)

CREATE AN OBJECT​

This creates a file ​​hello.txt​​​ from the file named ​​my_hello.txt​​:

with open('hello.txt', 'r') as hello_file:
conn.put_object(container_name, 'hello.txt',
cnotallow= hello_file.read(),
content_type='text/plain')

LIST OWNED CONTAINERS​

This gets a list of containers that you own, and prints out the container name:

for container in conn.get_account()[1]:
print(container['name'])

The output will look something like this:

mahbuckat1
mahbuckat2
mahbuckat3

LIST A CONTAINER’S CONTENT​

This gets a list of objects in the container, and prints out each object’s name, the file size, and last modified date:

for data in conn.get_container(container_name)[1]:
print('{0}\t{1}\t{2}'.format(data['name'], data['bytes'], data['last_modified']))

The output will look something like this:

myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

RETRIEVE AN OBJECT​

This downloads the object ​​hello.txt​​​ and saves it in ​​./my_hello.txt​​:

obj_tuple = conn.get_object(container_name, 'hello.txt')
with open('my_hello.txt', 'w') as my_hello:
my_hello.write(obj_tuple[1])

DELETE AN OBJECT​

This deletes the object ​​hello.txt​​:

conn.delete_object(container_name, 'hello.txt')

DELETE A CONTAINER

conn.delete_container(container_name)

RUBY SWIFT EXAMPLES

CREATE A CONNECTION​

This creates a connection so that you can interact with the server:

require 'cloudfiles'
username = 'account_name:user_name'
api_key = 'your_secret_key'

conn = CloudFiles::Connection.new(
:username => username,
:api_key => api_key,
:auth_url => 'http://objects.dreamhost.com/auth'
)

CREATE A CONTAINER​

This creates a new container called ​​my-new-container​

container = conn.create_container('my-new-container')

CREATE AN OBJECT​

This creates a file ​​hello.txt​​​ from the file named ​​my_hello.txt​

obj = container.create_object('hello.txt')
obj.load_from_filename('./my_hello.txt')
obj.content_type = 'text/plain'

LIST OWNED CONTAINERS​

This gets a list of Containers that you own, and also prints out the container name:

conn.containers.each do |container|
puts container
end

The output will look something like this:

mahbuckat1
mahbuckat2
mahbuckat3

LIST A CONTAINER’S CONTENTS​

This gets a list of objects in the container, and prints out each object’s name, the file size, and last modified date:

require 'date'  # not necessary in the next version

container.objects_detail.each do |name, data|
puts "#{name}\t#{data[:bytes]}\t#{data[:last_modified]}"
end

The output will look something like this:

myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

RETRIEVE AN OBJECT​

This downloads the object ​​hello.txt​​​ and saves it in ​​./my_hello.txt​​:

obj = container.object('hello.txt')
obj.save_to_filename('./my_hello.txt')

DELETE AN OBJECT​

This deletes the object ​​goodbye.txt​​:

container.delete_object('goodbye.txt')

DELETE A CONTAINER​

Note

The container must be empty! Otherwise the request won’t work!

container.delete_container('my-new-container')


举报

相关推荐

0 条评论