Last active
January 20, 2021 20:25
-
-
Save apeckham/1015ad86d95f62d4128b to your computer and use it in GitHub Desktop.
Sending a file upload to s3 with Fog, and writing a spec with Fog's built-in mocks, on Rails 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| storage = Fog::Storage.new( | |
| provider: 'AWS', | |
| aws_access_key_id: 'xxx', | |
| aws_secret_access_key: 'yyy', | |
| path_style: true | |
| ) | |
| directory = storage.directories.get('yourbucket') | |
| uploaded = directory.files.create( | |
| key: "324324.jpg", | |
| body: params[:uploaded_file], | |
| public: true | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ENV['AWS_ACCESS_KEY_ID'] = 'xxx' | |
| ENV['AWS_SECRET_ACCESS_KEY'] = 'yyy' | |
| Fog.mock! | |
| Fog::Mock.reset | |
| @s3 = Fog::Storage.new( | |
| provider: 'AWS', | |
| aws_access_key_id: 'xxx', | |
| aws_secret_access_key: 'yyy', | |
| path_style: true # for bucket names containing periods | |
| ) | |
| @s3.directories.create(key: 'yourbucket') | |
| post :receive_file, uploaded_file: fixture_file_upload('yaketyyak.jpg', 'some/mimetype') | |
| bucket = @s3.directories.get('yourbucket') | |
| expect(bucket.files.count).to eq 1 | |
| file = bucket.files.get("324324.jpg") | |
| expect(file).to be_present | |
| expect(file.body).to eq File.read('spec/fixtures/yaketyyak.jpg') | |
| expect(file.public_url).to eq 'http://bucketname/324234.jpg' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment