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;
}
}
CodePudding user response:
This is an alternate answer for those who don't actually want to download it
Simply replace the line:
processDownload( fileName, link, (int)info.Size );
WITH
System.Diagnostics.Process.Start( link );
This opens the PDF in your browser
CodePudding user response:
Ok, after much frustration and searching I have succeeded
C# Experts, please tell me what I don't need, I'm an android developer :)
Here is the answer
// ----------------------------------------------------------------------------
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 httpWebRequest =
(HttpWebRequest)WebRequest.Create( link );
HttpWebResponse httpResponse =
(HttpWebResponse)httpWebRequest.GetResponse();
HttpContext.Current = new HttpContext(
new HttpRequest( link, link, "" ),
new HttpResponse( new StringWriter() )
);
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ClearHeaders();
response.Buffer = true;
response.ContentType = "application/pdf";
response.AddHeader( "Content-Disposition",
"attachment; filename=\"" finalFileName "\"" );
byte[] buffer = new byte[size];
int bytesRead = 0;
string userProfile = System.Environment.GetEnvironmentVariable( "USERPROFILE" );
string downloadsFolder = userProfile "/Downloads/";
FileStream fileStream = File.Create( downloadsFolder finalFileName );
Stream httpResponseStream = httpResponse.GetResponseStream();
while( ( bytesRead = httpResponseStream.Read( buffer, 0, size ) ) != 0 )
{
fileStream.Write( buffer, 0, bytesRead );
}
fileStream.Flush();
fileStream.Close();
response.Flush();
response.End();
httpResponse.Close();
}
catch( WebException we )
{
MessageBox.Show( we.Message );
}
}
}
Thanks to everyone who helped