Thanks in Advance
I'm trying to download a file, specifically a PDF file, from firebase using C#
And it keeps throwing the exception: "The remote server returned an error: (403) Forbidden."
I have tried converting the Link to a Uri, I have tried hard-coding it to "gs://xxxxxxx.appspot.com/PDF/MyPDF.pdf"
Note The two lines added didn't help
What am I doing wrong
Here is the code:
// ----------------------------------------------------------------------------
private async Task download( string folder, string fileName )
{
FirebaseStorage storage = new FirebaseStorage( Bucket,
new FirebaseStorageOptions
{
AuthTokenAsyncFactory = () => Task.FromResult(
fireBaseAuthLink.FirebaseToken ),
ThrowOnCancel = true,
} );
var fileRef = storage.Child( folder ).Child( fileName );
string link = "";
try
{
link = await fileRef.GetDownloadUrlAsync();
var info = await fileRef.GetMetaDataAsync();
processDownload( fileName, link, (int)info.Size );
}
catch( Exception we )
{
MessageBox.Show( we.Message );
}
}
// ----------------------------------------------------------------------------
private void processDownload( string finalFileName, string link, int size )
{
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create( link );
httpRequest.Method = "GET";
// These two lines added to try to get it to work ----------------
httpRequest.UseDefaultCredentials = true;
httpRequest.Proxy.Credentials =
System.Net.CredentialCache.DefaultCredentials;
// ---------------------------------------------------------------
httpRequest.ContentType = "application/pdf; encoding='utf-8'";
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
// Define buffer and buffer size
byte[] buffer = new byte[size];
int bytesRead = 0;
// Read from response and write to file
FileStream fileStream = File.Create( finalFileName );
while( ( bytesRead = httpResponseStream.Read( buffer, 0, size ) ) != 0 )
{
fileStream.Write( buffer, 0, bytesRead );
}
}
catch( WebException we )
{
MessageBox.Show( we.Message );
}
}
And The firebase rules are
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /PDF/{allPaths=**} {
allow get;
allow read;
allow write;
}
}