Skip to content

Commit 75eeb0c

Browse files
committed
Added all non-Java examples
0 parents  commit 75eeb0c

8 files changed

Lines changed: 305 additions & 0 deletions

File tree

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
*.gem
2+
*.rbc
3+
.DS_Store
4+
/.config
5+
/coverage/
6+
/InstalledFiles
7+
/pkg/
8+
/spec/reports/
9+
/test/tmp/
10+
/test/version_tmp/
11+
/tmp/
12+
/chargeback/*.csv
13+
14+
## Java specific ignores
15+
.gradle/
16+
.settings/
17+
.classpath
18+
.project
19+
20+
## Specific to RubyMotion:
21+
.dat*
22+
.repl_history
23+
build/
24+
25+
## Documentation cache and generated files:
26+
/.yardoc/
27+
/_yardoc/
28+
/doc/
29+
/rdoc/
30+
31+
## Environment normalisation:
32+
/.bundle/
33+
/lib/bundler/man/
34+
35+
# for a library or gem, you might want to ignore these files since the code is
36+
# intended to run in multiple environments; otherwise, check them in:
37+
# Gemfile.lock
38+
# .ruby-version
39+
# .ruby-gemset
40+
41+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
42+
.rvmrc

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# StorageGRID Webscale Code Examples
2+
3+
This repository contains short code examples on how to use NetApp StorageGRID Webscale's API endpoints (S3 and Swift).
4+
5+
## Examples
6+
* S3 via Ruby (using AWS SDK for Ruby Version 2)
7+
* S3 via Python (using Boto 3)
8+
* S3 via Java (using AWS SDK for Java)
9+
* Swift via Python (using phyton-swiftclient)
10+
11+
More will follow!
12+
13+
## Compatibility
14+
All shown examples are compatible with StorageGRID Webscale 10.2 (GA Release). For older versions, please switch to the different branches.
15+

s3-python/examples.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import boto3
2+
import boto3.session
3+
4+
session = boto3.session.Session(profile_name='my_profile')
5+
endpoint = 'https://s3.mycompany.com:8082'
6+
7+
'''
8+
Do not use this in production - disabling SSL verification is discouraged!
9+
When using a self-signed certificate, make sure to pass it into the constructor:
10+
11+
s3 = session.resource(service_name='s3', endpoint_url=endpoint, verify='server_cert.pem')
12+
'''
13+
14+
s3 = session.resource(service_name='s3', endpoint_url=endpoint, verify=False)
15+
client = s3.meta.client
16+
17+
'''
18+
Bucket related operations
19+
'''
20+
21+
# Create new bucket for S3 account
22+
s3.Bucket('my-bucket').create()
23+
24+
# List all buckets for S3 account
25+
for bucket in s3.buckets.all():
26+
print(bucket.name)
27+
28+
# Delete bucket
29+
s3.Bucket('my-bucket').delete()
30+
31+
'''
32+
Object related operations
33+
'''
34+
35+
# Put a new object to a bucket
36+
obj = s3.Object('test', 'my-key')
37+
obj.put(Body='This is my object\'s data',
38+
Metadata={'customerid': '1234', 'location': 'germany'},
39+
ServerSideEncryption='AES256')
40+
41+
# Put object directly from a file
42+
#obj.upload_file('source-file', ExtraArgs={'Metadata': {'customer_id': '42'}, 'ServerSideEncryption': 'AES256'})
43+
44+
# Get an object directly to a file
45+
#obj.download_file('target-file')
46+
47+
# Copy an existing object
48+
copied_obj = s3.Object('test', 'my-copied-key')
49+
copied_obj.copy_from(CopySource='/test/my-key')
50+
51+
# Get object from bucket
52+
response = obj.get()
53+
data = response['Body'].read()
54+
metadata = response['Metadata']
55+
print("Data: %s // Metadata: %s" % (data, metadata))
56+
57+
# List all objects for a bucket
58+
for obj in s3.Bucket('test').objects.all():
59+
print(obj.key)
60+
61+
# Generate a pre-signed URL (only possible via client, not directly via Object object)
62+
url = client.generate_presigned_url('get_object', {'Bucket': 'test', 'Key': 'my-key'}, ExpiresIn=3600)
63+
print("Pre-signed URL: %s" % (url))
64+
65+
# Delete the object from its bucket
66+
obj.delete()
67+

s3-ruby/Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'aws-sdk', '~> 2'

s3-ruby/Gemfile.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
aws-sdk (2.0.28)
5+
aws-sdk-resources (= 2.0.28)
6+
aws-sdk-core (2.0.28)
7+
builder (~> 3.0)
8+
jmespath (~> 1.0)
9+
multi_json (~> 1.0)
10+
multi_xml (~> 0.5)
11+
aws-sdk-resources (2.0.28)
12+
aws-sdk-core (= 2.0.28)
13+
builder (3.2.2)
14+
jmespath (1.0.2)
15+
multi_json (~> 1.0)
16+
multi_json (1.10.1)
17+
multi_xml (0.5.5)
18+
19+
PLATFORMS
20+
ruby
21+
22+
DEPENDENCIES
23+
aws-sdk (~> 2)

s3-ruby/examples.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env ruby
2+
require 'aws-sdk'
3+
4+
endpoint = 'https://s3.mycompany.com:8082'
5+
credentials = Aws::SharedCredentials.new(profile_name: 'my_profile')
6+
7+
# Notes on certificate usage with StorageGRID Webscale
8+
# ----------------------------------------------------
9+
# Set ssl_verfiy_peer to true if StorageGRID's certificate is CA-signed or if you want to use a self-signed certificate
10+
# If you use a self-signed certificate, set ssl_ca_bundle to the self-signed certificate
11+
12+
client = Aws::S3::Client.new(region: 'us-east-1',
13+
endpoint: endpoint,
14+
credentials: credentials,
15+
force_path_style: true,
16+
ssl_verify_peer: false,
17+
#ssl_ca_bundle: 'server_cert.crt'
18+
)
19+
20+
# Use resource style access
21+
s3 = Aws::S3::Resource.new(client: client)
22+
23+
# Bucket related operations
24+
# -------------------------
25+
26+
# List buckets
27+
s3.buckets.each do |bucket|
28+
puts "Bucket: #{bucket.name}"
29+
puts " -> created: #{bucket.creation_date}"
30+
end
31+
32+
# Create bucket
33+
s3.bucket('new-bucket').create
34+
35+
# Delete bucket
36+
s3.bucket('new-bucket').delete
37+
38+
# Object related operations
39+
# -------------------------
40+
41+
# Create Object
42+
s3.bucket('test').object('my_object').put(
43+
metadata: {
44+
'mykey1' => 'myvalue1',
45+
'mykey2' => 'myvalue2'
46+
},
47+
body: 'Hello, I\'m the object\'s data!',
48+
# encrypt object if desired
49+
server_side_encryption: 'AES256'
50+
)
51+
52+
# Copy existing Object
53+
s3.bucket('test').object('copied_object').copy_from('test/my_object')
54+
55+
# List objects
56+
s3.bucket('test').objects.each do |object|
57+
puts "Object key: #{object.key}"
58+
puts " -> Size: #{object.size} bytes"
59+
puts " -> Last modified: #{object.last_modified}"
60+
end
61+
62+
# Get object
63+
get_response = s3.bucket('test').object('my_object').get
64+
puts "Object content: #{get_response.body.string}"
65+
66+
# Get only bytes 15 to 20 of the object
67+
get_response = s3.bucket('test').object('my_object').get({
68+
range: "bytes=15-20"
69+
})
70+
puts "Object byte 15-20 content: #{get_response.body.string}"
71+
72+
# Get object metadata and size
73+
metadata = s3.bucket('test').object('my_object').metadata
74+
size = s3.bucket('test').object('my_object').content_length
75+
puts "Object metadata: #{metadata}"
76+
puts "Object content length: #{size}"
77+
78+
# Generate a pre-signed URL that is valid for one hour
79+
url = s3.bucket('test').object('my_object').presigned_url(:get, expires_in: 3600)
80+
puts url
81+
82+
# Delete object
83+
s3.bucket('test').object('my_object')

swift-python/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Prerequisites
2+
3+
This examples requires swiftclient, which can be installed via:
4+
5+
`sudo pip install python-swiftclient`

swift-python/examples.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import swiftclient
2+
3+
username = '57347988074919328399:swiftadmin'
4+
password = 'supersecret'
5+
authurl = 'https://swift.mycompany.com:8083/auth/v1.0'
6+
7+
swift = swiftclient.client.Connection(auth_version='1', user=username, key=password, insecure=True, authurl=authurl)
8+
9+
'''
10+
If you are using self-signed certificates, you can use the following code:
11+
12+
cacert = '/path/to/server/cert'
13+
swift = swiftclient.client.Connection(auth_version='1', user=username, key=password, cacert=cacert, authurl=authurl)
14+
'''
15+
16+
container = 'test-container'
17+
obj_key = 'test-object'
18+
19+
# Get authentication information
20+
print(swift.get_auth())
21+
22+
# Create container
23+
swift.put_container(container)
24+
25+
# List all containers and account information
26+
response = swift.get_account()
27+
account_info = response[0]
28+
containers = response[1]
29+
30+
print "Bytes used by account: ", account_info['x-account-bytes-used']
31+
print "Number of containers in account: ", account_info['x-account-container-count']
32+
print "Number of objects in account: ", account_info['x-account-object-count']
33+
34+
for c in containers:
35+
name = c['name']
36+
num_objects = c['count']
37+
size = c['bytes']
38+
print("Container name: %s (total: %s objects, %s bytes)" % (name, num_objects, size))
39+
40+
# Get information of a single container
41+
response = swift.get_container(container)
42+
print "Container: ", response
43+
44+
# Put object into container
45+
swift.put_object(container, obj_key,
46+
contents='This is my object\'s content',
47+
headers={'X-Object-Meta-CustomerID':'42',
48+
'X-Object-Meta-Color':'red'})
49+
50+
# List contents of a container
51+
for obj in swift.get_container(container)[1]:
52+
print "Object key: ", obj['name']
53+
print "Object size: ", obj['bytes']
54+
print "Object last modified: ", obj['last_modified']
55+
56+
# Get object from container
57+
response = swift.get_object(container, obj_key)
58+
object_headers = response[0]
59+
object_content = response[1]
60+
print "Object headers: ", object_headers
61+
print "Object content: ", object_content
62+
63+
# Delete object
64+
swift.delete_object(container, obj_key)
65+
66+
# Delete container
67+
swift.delete_container(container)

0 commit comments

Comments
 (0)