'Firebase Storage returning SignatureDoesNotMatch on accessing uploaded link
I have uploaded some documents to google cloud storage and stored the returned signed URL in the database. Later we found that some URL seems to be corrupted and shows the following error when we try to open the link.
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Message>
<StringToSign>GET 1675296000 /projectt2.com/user%2FQf%2Finferences%767876%2FReport</StringToSign>
</Error>
Also, we have identified that some of the documents will not have any access token in the firebase storage but the others which are valid will have the same. Both these files are created from the same source code. (There are other documents that are of other modules which we can view even without access token)
Here is the code snippet for converting the file
const pdf = await request
.get({
uri: document.filePath,
encoding: null,
headers: {
"Content-type": (document?.filePath?.toUpperCase().includes('PDF')) ? "text/pdf" : "image/jpeg",
}
});
const mime_type = (document?.filePath?.toUpperCase().includes('PDF')) ? 'application/pdf' : 'image/jpeg'
const imageData = await addFileToStorage(pdf, `user/${uid}/inferences/${inference_id}/${document.specifiedType}`, mime_type, 365)
const urls = await imageData.url;
return {
type: document.specifiedType,
url: urls?.length ? urls[0] : '',
path: (imageData) ? imageData.path : ''
}
Code for Cloud storage file upload addFileToStorage
import {config} from "firebase-functions";
import {storage} from "../../config/config";
import * as moment from "moment-timezone";
export const bucket = storage.bucket(config().google.storage_url);
export const addFileToStorage = (data: any, path: string, mime_type: string, expiry: number = 1) => {
const currentDate = new Date();
currentDate.setDate(currentDate.getDate() + expiry);
const expiryDate = moment.tz(currentDate, 'Europe/London').format('MM-DD-YYYY');
const file = bucket.file(path);
const fileOptions = {contentType: mime_type};
return file.save(data, fileOptions)
.then(() => {
return {
url: file.getSignedUrl({action: 'read', expires: expiryDate}),
path
}
});
}
I am not sure is that the issue related to uploading or anything related to storage url.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
