Jon
Jon

Reputation: 833

Extra object in Google Cloud Storage when using Fog

I am following the fog.io/storage example for creating a directory and then uploading a file. Everything works great when I push my file into Google Cloud Storage except there is always a "binary/octet-stream" file named exactly as the deepest file I create.

My code is very similar to the AWS example in that I create a directory and from that new directory, I create a file. The directory structure is created properly and the file is uploaded properly but there is always an extra, 0-byte file. My code looks like:

job_number = 100

connection = Fog::Storage.new({
  :provider                         => 'Google',
  :google_storage_access_key_id     => YOUR_GCE_ACCESS_KEY_ID,
  :google_storage_secret_access_key => YOUR_GCE_SECRET_ACCESS_KEY
})

directory = connection.directories.create(
  :key    => "test-project/uploads/#{job_number}",
  :public => false
)

file = directory.files.create(
  :key    => 'file.pdf',
  :content_type => 'application/pdf',
  :body   => File.open("/path/to/my/file.pdf"),
  :public => false
)

The directory structure is perfect (gs://test-project/uploads/100 folder exists) and the file.pdf file exists in that directory as well (gs://test-project/uploads/100/file.pdf).

The problem is that after the:

directory = connection.directories.create(
  :key    => "test-project/uploads/#{job_number}",
  :public => false
)

command runs, there is a file at gs://test-project/uploads/100 as well as a directory gs://test-project/uploads/100/. When I walk through the code, the connection.directories.create(...) command is definitely creating the extra file but I cannot figure out why.

I have also tried to add a trailing slash to the key value for the connection.directories.create(...) command but that actually creates a different directory structure problem that is worse than this (this isn't bad, just annoying).

Has anyone seen this or know how to correctly have the directory structure created through Fog?

Upvotes: 1

Views: 598

Answers (1)

Lister
Lister

Reputation: 170

Instead of creating the directory right up to the file, just create/get the base directory/bucket and then save the file with the rest of the directory structure. So it would look like this:

job_number = 100

connection = Fog::Storage.new({
  :provider                         => 'Google',
  :google_storage_access_key_id     => YOUR_GCE_ACCESS_KEY_ID,
  :google_storage_secret_access_key => YOUR_GCE_SECRET_ACCESS_KEY
})

directory = connection.directories.create(
  :key    => "test-project",
  :public => false
)

file = directory.files.create(
  :key    => 'uploads/#{job_number}/file.pdf',
  :content_type => 'application/pdf',
  :body   => File.open("/path/to/my/file.pdf"),
  :public => false
)

Upvotes: 1

Related Questions