0

When I try to access the generated URL, some header values error appears. Based on other related question, was suggested to append the shared access signature to the URL. Now when I try to access the URL, some authentication error appears. What I am doing wrong?

This is what I've tried:

export default (fileData) => {
  let fileToUpload = `boleto.pdf`;
  let shareName = `boleto-` + uuid.v1();
  let directoryName = `boletos`;
  let fileName = fileToUpload;
  let startDate = new Date();
  let expiryDate = new Date(startDate);

  const fileService = storage.createFileService();

  const sasToken = fileService.generateSharedAccessSignature(shareName, directoryName, fileName, {
    AccessPolicy: {
      Permissions: storage.FileUtilities.SharedAccessPermissions.READ,
      Start: startDate,
      Expiry: expiryDate
    }
  });

  const sharedFileService = storage.createFileService(`DefaultEndpointsProtocol=https;AccountName=${process.env.AZURE_STORAGE_ACCOUNT};AccountKey=${process.env.AZURE_STORAGE_KEY};EndpointSuffix=core.windows.net`);

  sharedFileService.createShareIfNotExists(shareName, (err) => {
    if (err) throw new Error(err);
    sharedFileService.createDirectoryIfNotExists(shareName, directoryName, (err) => {
      if (err) throw new Error(err);
      let nextDirectoryName = directoryName + `/` + directoryName + `01`;
      fileService.createDirectoryIfNotExists(shareName, nextDirectoryName, (err) => {
        if (err) throw new Error(err);
        sharedFileService.createFileFromStream(shareName, directoryName, fileName, fileData.stream, fileData.buffer.length, (err, result, response) => {
          if (err) throw new Error(err);
          console.log(`FILE UPLOADED!`);
          const url = sharedFileService.getUrl(shareName, directoryName, fileName);
          console.log(`URL: ${JSON.stringify(url)}?sv=${sasToken}`);
        });
      });
    });
  });
};
gcfabri
  • 564
  • 2
  • 7
  • 28
  • I couldn't make creating file share programmatically working. It throws 404 bad request when the try to create one through code. Strange. Anyway, not sure how to do it in Node.js but you can grant access to resources for a limited time by following code on this page, under Generate Shared Access heading. Its just a few lines of code and can easily be rewritten in Node js – Allen King Nov 27 '17 at 02:03
  • Can you edit your question and include the error message you’re getting? Also share the SDK version. – Gaurav Mantri Nov 27 '17 at 02:25

1 Answers1

1

I see two issues with your code:

  1. Your startDate and expiryDate are the same and that too set as current date. What this will do is make your sas token expire as soon as it is created. You should set expiry date to a date/time value in future.
  2. You're including sv parameter in your query string which is not needed as sv is already included in your sas token.

    console.log(URL: ${JSON.stringify(url)}?sv=${sasToken});

Please change this to something like the following:

console.log(`URL: ${url}?${sasToken}`);

This URL should not give you authentication error.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • now this error is shown: `InvalidHeaderValueThe value for one of the HTTP headers is not in the correct format. RequestId:a71bb7c2-001a-0025-492c-673e73000000 Time:2017-11-27T03:04:32.9028052Zx-ms-version` – gcfabri Nov 27 '17 at 03:05
  • Now it works. I wass missing the '?'. Thank you Gaurav! – gcfabri Nov 27 '17 at 03:09