Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

How to distinguish between files and directories in Google Cloud Storage

In Google Cloud Storage, I tried to get a list of files in a subdirectory and process those files.

However, when I looked at the error message that the file was wrong, I found that the list of files retrieved included a subdirectory.

Reason

I've gotten into this misunderstanding before, but there is no real directory for this type of cloud storage, not just Google, but AWS, Azure, etc.

The full path is the official name of the file, and the UI only represents the part separated by "/" as a pseudo directory.

Click here for an explanation of that area.

www.ekwbtblog.com

empty directory

The Google Cloud Storage portal UI allows you to create directories only.

It is strange that a directory can be created even though there should be no concept of a directory. In fact, when a directory was created in the UI, a file with "\/" was created. (The "/" at the end of the name is the secret.)

Also, the UI makes it look like a directory if there is a file with "\/".

The reason we got stuck this time was that we first created a directory in the UI and then put a file in it, so the directory was acquired as a file and tried to process it.

Solution

Now, to the main question, how to distinguish whether the retrieved file is a file or a directory,

Identified by whether the file name ends in "/" or not

will be.

Investigation to see if this is really the case

  • The only way to judge is by name!
  • I think the identifying information is in the attributes of the retrieved file!

I could hardly believe it, so I tested it.

Create a file on Google Cloud Storage whose name ends with "test/" and write "It's TEST! and then read that Google Cloud Storage file again to display its contents.

import { Storage } from '@google-colud/storage';

const storage = new Storage();
const bucket = storage.bucket('xxxx');

async function writeDirectory(){
    const file = await bucket.file('test/');
    const stream = file.createWriteStream();

    return new Promise((resolve, reject)=>{
        stream.on('finish', ()=> resolve());
        stream.write('TESTですよ!');
        stream.end();
    });
}

async function readDirectory(){
    const file = await bucket.file('test/');
    const stream = file.createReadStream();
    
    return new Promise((resolve, reject)=>{
        stream.on('end', ()=> resolve());
        stream.on('data', (data)=>{
            console.log(data.toString());
        });
    });
}

async (()=>{

    console.log('writeDirectory');
    await writeDirectory();

    console.log('readDirectory');
    await readDirectory();

})();

result

I uploaded a file and now I have a "test/" directory.

How to distinguish between files and directories in Google Cloud Storage

When I displayed the contents of the "test" directory, it was a file of what I had written....

writeDirectory
readDirectory
TESTですよ!

Even though it is a directory, there is no way to distinguish it other than by name, since it is not wrong as a file.

Conclusion.

A "/" at the end of a file name is a directory!

Impressions, etc.

It is tedious, but when you get a file, you should always check if the filename ends with a "/".

If you look carefully, you will see it in the documentation....

www.ekwbtblog.com [https://www.kwbtblog.com/entry/2019/07/17/170920:embed:cite] [https://www.kwbtblog.com/entry/2018/12/14/015706:embed:cite]

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com