• This repository has been archived on 20/Nov/2022
  • Stars
    star
    70
  • Rank 447,840 (Top 9 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Unofficial AWS SDK for Crystal

aws

Unofficial AWS SDK integration for Crystal.

Status: This is still very much WIP (Work in Progress).

Installation

Add this to your application's shard.yml:

dependencies:
  aws:
    github: sdogruyol/aws

Usage

SQS

require "aws"

KEY    = "your-aws-key"
SECRET = "your-aws-secret"
REGION = "eu-west-1"

client = Aws::Sqs::Client.new(REGION, KEY, SECRET)

# Create a queue first
client.create_queue("sqs-crystal")

# Send a message to previously created queue
client.send_message("sqs-crystal", "Hi from Crystal!")

S3

client = Aws::S3::Client.new("us-east1", "key", "secret")

For S3 compatible services, like DigitalOcean Spaces or Minio, you'll need to set a custom endpoint:

client = Aws::S3::Client.new("nyc3", "key", "secret", endpoint: "https://nyc3.digitaloceanspaces.com")

If you wish you wish to you version 2 request signing you may specify the signer

client = Aws::S3::Client.new("us-east1", "key", "secret", signer: :v2)

List Buckets

resp = client.list_buckets
resp.buckets # => ["bucket1", "bucket2"]

Delete a bucket

client = Client.new("region", "key", "secret")
resp = client.delete_bucket("test")
resp # => true

Create a bucket

client = Client.new("region", "key", "secret")
resp = client.create_bucket("test")
resp # => true

Put Object

resp = client.put_object("bucket_name", "object_key", "myobjectbody")
resp.etag # => ...

You can also pass additional headers (e.g. metadata):

client.put_object("bucket_name", "object_key", "myobjectbody", {"x-amz-meta-name" => "myobject"})

Delete Object

resp = client.delete_object("bucket_name", "object_key")
resp # => true

Check Bucket Existence

resp = client.head_bucket("bucket_name")
resp # => true

Raises an exception if bucket does not exist.

Batch Delete Objects

resp = client.batch_delete("bucket_name", ["key1", "key2"])
resp.success? # => true

Get Object

resp = client.put_object("bucket_name", "object_key")
resp.body # => myobjectbody

List Objects

client.list_objects("bucket_name").each do |resp|
  p resp.contents.map(&.key)
end

Upload a file

uploader = Aws::S3::FileUploader.new(client)

File.open(File.expand_path("myfile"), "r") do |file|
  puts uploader.upload("bucket_name", "someobjectkey", file)
end

You can also pass additional headers (e.g. metadata):

uploader = Aws::S3::FileUploader.new(client)

File.open(File.expand_path("myfile"), "r") do |file|
  puts uploader.upload("bucket_name", "someobjectkey", file, {"x-amz-meta-name" => "myobject"})
end

Creating a Presigned::Form.

form = Aws::S3::Presigned::Form.build("us-east-1", "access key", "secret key") do |form|
  form.expiration(Time.utc_now.to_unix + 1000)
  form.condition("bucket", "mybucket")
  form.condition("acl", "public-read")
  form.condition("key", SecureRandom.uuid)
  form.condition("Content-Type", "text/plain")
  form.condition("success_action_status", "201")
end

You may use version 2 request signing via

form = Aws::S3::Presigned::Form.build("us-east-1", "access key", "secret key", signer: :v2) do |form|
  ...
end

Converting the form to raw HTML (for browser uploads, etc).

puts form.to_html

Submitting the form.

data = IO::Memory.new("Hello, S3!")
form.submit(data)

Creating a Presigned::Url.

options = Aws::S3::Presigned::Url::Options.new(
   aws_access_key: "key",
   aws_secret_key: "secret",
   region: "us-east-1",
   object: "test.txt",
   bucket: "mybucket",
   additional_options: {
  "Content-Type" => "image/png"
})

url = Aws::S3::Presigned::Url.new(options)
p url.for(:put)

You may use version 2 request signing via

options = Aws::S3::Presigned::Url::Options.new(
   aws_access_key: "key",
   aws_secret_key: "secret",
   region: "us-east-1",
   object: "test.txt",
   bucket: "mybucket",
   signer: :v2
)

Contributing

  1. Fork it (https://github.com/sdogruyol/aws/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • sdogruyol Serdar Dogruyol - creator, maintainer

Thanks

Thanks to @taylorfinnell for his work on https://github.com/taylorfinnell/awscr-signer and https://github.com/taylorfinnell/awscr-s3.

More Repositories

1

awesome-ruby

A curated list of awesome Ruby frameworks, libraries and resources. Inspired by awesome-php & awesome-python.
1,247
star
2

fast-http-server

Super fast, zero configuration command line HTTP Server.
Crystal
148
star
3

cryload

HTTP benchmarking tool written in Crystal
Crystal
121
star
4

tren

Give your SQL some love
Crystal
115
star
5

kemal-chat

Sample application to demonstrate how easy it is to build realtime applications with Kemal using WebSocket.
HTML
63
star
6

cloud_backup

Easily dump your database and store it in the cloud.
Ruby
38
star
7

gencleredestek.com

gencleredestek.com website source code
Ruby
36
star
8

crystal-weekly

Crystal Weekly Website
SCSS
30
star
9

friends-of-crystal

Organizations running Crystal in production
HTML
30
star
10

capistrano-kemal

Capistrano integration for Kemal
Ruby
25
star
11

cuba-generator

Application Generator for Cuba framework
Ruby
23
star
12

crystal_for_rubyists

Crystal For Rubyists
HTML
23
star
13

kemal-github-api

Github API routes benchmark for Kemal
Crystal
21
star
14

kemal-pg

Easily add Postgresql database to Kemal
Crystal
21
star
15

crystal-for-rubyists-en

20
star
16

kemal-mysql

Easily add MySQL database to your Kemal application.
Crystal
14
star
17

safran

Safran için Ruby ile yazılmış Command Line okuyucusu.
Ruby
13
star
18

rust-vs-ruby

Sample applications for 'Rust vs Ruby: Building an API' blog post
Rust
11
star
19

kemal-showdown

🏇 benchmark Kemal with Sinatra-like web frameworks
JavaScript
10
star
20

multipart

Multipart Form Data parser for Crystal
Crystal
10
star
21

safran-api

Safran Unofficial API
Ruby
8
star
22

amethyst-bin

Bash script for automatically setting up an Amethyst skeleton project including dependencies
Shell
7
star
23

calculator-tdd

An example of applied TDD with a simple calculator
Ruby
7
star
24

cuba-datamapper-sample

Sample application to show the simplicity and power of Cuba
Ruby
6
star
25

lyk2017-blog

Ruby
6
star
26

kemal-redis

Easily add Redis to Kemal
Crystal
6
star
27

sdogruyol.github.io

serdardogruyol.com source code
SCSS
5
star
28

hn_reader

Hacker News Reader Gui built with Jruby
Ruby
5
star
29

kemal-blog

CSS
4
star
30

russh

Russh: Ruby + SSH. SSH Config Management the Ruby way.
Ruby
4
star
31

kemal-pg-sample

Sample app to demonstrate kemal + postgresql usage
Crystal
4
star
32

critcoin

Crystal
3
star
33

crystal-for-rubyists-cn

Crystal for Rubyists Chinese Version
2
star
34

lyk2015-ruby101

Linux Yaz Kampı 2015 Ruby 101 içeriği
JavaScript
1
star
35

elixir_schizo

Example project from Elixir Sips
Elixir
1
star
36

kemal-multi-path

Crystal
1
star
37

erc20-tracker

Simple ERC20 token tracker
JavaScript
1
star
38

kemalturkiye.org

HTML
1
star
39

homebrew-cryload

Brew tap for Cryload
Ruby
1
star