#!/usr/bin/env ruby
# 通过restful API
require "base64"
require "openssl"
require "net/http"
PublicKey = "publicKey"
PrivateKey = "privateKey"
def gen_auth(http_method, bucket, filename, gmttime)
sign = http_method + "\n" + "\n" + "\n" + gmttime + "\n" + "x-amz-acl:public-read" + "\n" + "/" + bucket + "/" + filename
puts sign
hmac = OpenSSL::HMAC.digest "sha1", PrivateKey, sign
base64Str = Base64.encode64 hmac
"AWS" + " " + PublicKey + ":" + base64Str
end
Host = '36.135.133.17'
Bucket = 'bk'
BucketFile = 'rb3'
def put_file
gmttime = Time.now.utc.strftime "%a, %d %b %Y %H:%M:%S GMT"
auth = gen_auth "PUT", Bucket, BucketFile, gmttime
headers = { 'Accept': '*/*', 'User-Agent': 'fuck', 'Content-Type': '', # for ceph Q version, Content-type header empty or no exist
'Accept-Encoding': 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', "Host": Host, "Date": gmttime, "x-amz-acl": "public-read", "Authorization": auth, "Content-Length": '12' }
http = Net::HTTP.new(Host, 32206)
res = http.put "/#{Bucket}/#{BucketFile}", "xour_put_dat",headers
puts "response code: #{res.code}, body: #{res.body}"
end
if $PROGRAM_NAME == __FILE__
put_file
end
# 通过SDK 文中。
# ak/sk 和 url和bucket 替换成可正常访问的
require "aws-sdk-s3"
require "resolv-replace"
def run_demo
Aws.config.update(
endpoint: "http://36.134.132.17:3367",
access_key_id: "ak",
secret_access_key: "sk",
force_path_style: true,
region: "us-east-1",
)
s3_client = Aws::S3::Client.new
# s3_client.create_bucket(bucket: 'my-new-bucket2')
s3_client.list_buckets.buckets.each do |bucket|
puts "#{bucket.name}\t#{bucket.creation_date}"
begin
objects = s3_client.list_objects(delimiter: "/", prefix: "", bucket: bucket.name)
puts objects.common_prefixes
rescue
puts $!
end
end
# resp = s3_client.put_object_acl(bucket: "bk", key: "load_snap_rec/srs1_0/1685049511.ts", acl: "public-read")
# puts resp.to_h
end
run_demo if $PROGRAM_NAME == __FILE__
标签:end,puts,s3,bucket,ceph,访问,put,http,ruby
From: https://www.cnblogs.com/agnt/p/17468381.html