step-up-labs / firebase-storage-dotnet

C# library for Firebase Storage
MIT License
143 stars 36 forks source link

List all files #32

Closed Go2sites closed 2 years ago

Go2sites commented 4 years ago

Hello, is there any way to list all files?

bezysoftware commented 4 years ago

No, that isn't supported currently

nmoschkin commented 4 years ago

I wrote an extension for this to list files.

If anyone wants it.... let me know.

(also wrote an extension to download files, too, with or without the token, as long as you're logged in)

maned3v commented 4 years ago

I wrote an extension for this to list files.

If anyone wants it.... let me know.

(also wrote an extension to download files, too, with or without the token, as long as you're logged in)

Hi @nmoschkin

I would love to have it, could you pastebin it? Thanks <3

nmoschkin commented 4 years ago

I wrote an extension for this to list files. If anyone wants it.... let me know. (also wrote an extension to download files, too, with or without the token, as long as you're logged in)

Hi @nmoschkin

I would love to have it, could you pastebin it? Thanks <3

Well ... it's sort of a work-in-progress... I haven't actually formalized it to work as the JavaScript library or as this FirebaseStorage component works ...

But I can paste what I have... Let me get back to you in a couple of days.

maned3v commented 4 years ago

I wrote an extension for this to list files. If anyone wants it.... let me know. (also wrote an extension to download files, too, with or without the token, as long as you're logged in)

Hi @nmoschkin I would love to have it, could you pastebin it? Thanks <3

Well ... it's sort of a work-in-progress... I haven't actually formalized it to work as the JavaScript library or as this FirebaseStorage component works ...

But I can paste what I have... Let me get back to you in a couple of days.

Thank you πŸ‘πŸ»

nmoschkin commented 4 years ago

@maned3v

And I finally got around to putting it all together!

I forked the project and committed my changes... see below for an example of the code in use.

My FirebaseStorage fork

The functions you want are two functions in FirebaseStorage: ListRootPrefixes, and ListAllFiles Each FirebaseStorageReference has the functions ListPrefixes and ListFiles.

I documented the functions in code.

One other thing. Google Cloud Storage API (upon which Firebase calls are based) has a limit of 1000 items per return, and if there's more items you get a nextPageToken. Call the ListPrefixes and ListFiles with the pageToken returned in the last call to get the next page of results. So to get an entire list, keep fetching until the nextPageToken is either null or the same as the last token or the total number of results is less than the maximum allowed number of results. I have documented this, somewhat, in code, as well. If you attempt to pass a maxResults outside the range of 1-1000 you will get an ArgumentOutOfRangeException that I have thrown.

All that being said, have fun, and enjoy!

I'm going to create a pull request, but I have no idea if it will be accepted or if this is the final form they want, but I'm going to attempt to, anyway. They haven't worked on this, in a while... so I hope this helps some people.

bpass1 = await Storage.ListRootPrefixes();

foreach (var prefix in bpass1.Prefixes)
{
    bpass2 = await Storage.Child(prefix)
         .Child("models")
         .Child("model-export")
         .Child("icn")
         .ListFiles(); 

    outBucket.AddRange(bpass2.Items);
}
maned3v commented 3 years ago

Thank you @nmoschkin awesome!

hoangatpaygate commented 3 years ago

@nmoschkin Seems all the list functions are not working anymore (ListFiles, ListPrefixes, ListRootPrefixes). It throws an exception "Response status code does not indicate success: 404 (Not Found)." I think the link was changed, please help to check. Thank you very much!

nmoschkin commented 3 years ago

Ah yes. There seems to have been a small change to the way they recognized projects.

You may pull the latest changes. It’s fixed (or should be fixed)

From: hoangatpaygate @.> Sent: Sunday, April 11, 2021 4:43 AM To: step-up-labs/firebase-storage-dotnet @.> Cc: Nathaniel Moschkin @.>; Mention @.> Subject: Re: [step-up-labs/firebase-storage-dotnet] List all files (#32)

@nmoschkinhttps://github.com/nmoschkin Seems all the list functions are not working anymore (ListFiles, ListPrefixes, ListRootPrefixes). It throws an exception "Response status code does not indicate success: 404 (Not Found)." I think the link was changed, please help to check. Thank you very much!

β€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/step-up-labs/firebase-storage-dotnet/issues/32#issuecomment-817271948, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABNDOEKQPAHJUB5US3UFLY3TIFOK5ANCNFSM4LJGZ4SA.

hoangatpaygate commented 3 years ago

Thank @nmoschkin very much! It is working now :)

marknasr22 commented 3 years ago

One other thing. Google Cloud Storage API (upon which Firebase calls are based) has a limit of 1000 items per return, and if there's more items you get a nextPageToken. Call the ListPrefixes and ListFiles with the pageToken returned in the last call to get the next page of results. So to get an entire list, keep fetching until the nextPageToken is either null or the same as the last token or the total number of results is less than the maximum allowed number of results.

@nmoschkin Hey, thank you for the changes, they work great. Do you mind showing some sample code for the pageToken part? It's a bit confusing, thanks.

hoangatpaygate commented 3 years ago

@marknasr22 Here is example code to download all the files in a folder

var pageToken = string.Empty;

do { var storageBucketList = storage.Child(folder).ListPrefixes(1000, pageToken).Result; foreach (var item in storageBucketList.Items) { var storageReference = item.GetReference(); var downloadUrl = storageReference.GetDownloadUrlAsync().Result; Task.Factory.StartNew(() => { var client = new HttpClient(); var resp = client.GetAsync(downloadUrl).Result;

        var content = resp.Content.ReadAsStringAsync().Result.Trim();
        if (!string.IsNullOrEmpty(content))
            File.WriteAllText(item.Name, content);
    }, cancellation.Token);
}

pageToken = storageBucketList.NextPageToken;
if (string.IsNullOrEmpty(pageToken)) break;

} while (true);

marknasr22 commented 3 years ago

Thank you @hoangatpaygate

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

Closing the issue due to inactivity. Feel free to re-open