在我的Rails项目中,我使用了gem 'google-cloud-storage'和gem 'aws-sdk-s3'。
要使用AWS创建文件,我使用以下代码:
connection = Fog::Storage.new({
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] })
directory = connection.directories.get(ENV['AWS_BUCKET'])
file = directory.files.create(
key: some_filename,
content_type: some_type,
body: some_body,
public: true )有没有办法也用gcloud创建一个文件?
发布于 2020-02-25 19:02:38
理解问题。
要创建文件,需要使用create_file方法
def create_file file, path = nil, acl: nil, cache_control: nil,
content_disposition: nil, content_encoding: nil,
content_language: nil, content_type: nil,
crc32c: nil, md5: nil, metadata: nil,
encryption_key: nil, encryption_key_sha256: nil
ensure_service!
options = { acl: File::Acl.predefined_rule_for(acl), md5: md5,
cache_control: cache_control, content_type: content_type,
content_disposition: content_disposition, crc32c: crc32c,
content_encoding: content_encoding,
content_language: content_language, metadata: metadata,
key: encryption_key, key_sha256: encryption_key_sha256 }
ensure_file_exists! file
path ||= Pathname(file).to_path
gapi = service.insert_file name, file, path, options
File.from_gapi gapi, service
end在我的例子中,我通过创建一个tempfile来加载声音,然后将这个tempfile放入方法中。
storage = Google::Cloud::Storage.new(project: ENV['GOOGLE_ID'],
keyfile: ENV['GOOGLE_JSON'] )
bucket = storage.bucket( ENV['GOOGLE_BUCKET'] )
bucket.create_file(sound.tempfile, some_filename)https://stackoverflow.com/questions/60391815
复制相似问题