file/samples/basic.sample.js
async function main() {
  // Enter your storage account name and shared key
  const account = "";
  const accountKey = "";

  // Use SharedKeyCredential with storage account and account key
  // SharedKeyCredential is only avaiable in Node.js runtime, not in browsers
  const sharedKeyCredential = new SharedKeyCredential(account, accountKey);

  // Use AnonymousCredential when url already includes a SAS signature
  const anonymousCredential = new AnonymousCredential();

  // Use sharedKeyCredential or anonymousCredential to create a pipeline
  const pipeline = StorageURL.newPipeline(sharedKeyCredential);

  // List shares
  const serviceURL = new ServiceURL(
    // When using AnonymousCredential, following url should include a valid SAS
    `https://${account}.file.core.windows.net`,
    pipeline
  );

  console.log(`List shares`);
  let marker;
  do {
    const listSharesResponse = await serviceURL.listSharesSegment(
      Aborter.none,
      marker
    );

    marker = listSharesResponse.nextMarker;
    for (const share of listSharesResponse.shareItems) {
      console.log(`\tShare: ${share.name}`);
    }
  } while (marker);

  // Create a share
  const shareName = `newshare${new Date().getTime()}`;
  const shareURL = ShareURL.fromServiceURL(serviceURL, shareName);
  await shareURL.create(Aborter.none);
  console.log(`Create share ${shareName} successfully`);

  // Create a directory
  const directoryName = `newdirectory${new Date().getTime()}`;
  const directoryURL = DirectoryURL.fromShareURL(shareURL, directoryName);
  await directoryURL.create(Aborter.none);
  console.log(`Create directory ${directoryName} successfully`);

  // Create a file
  const content = "Hello World!";
  const fileName = "newfile" + new Date().getTime();
  const fileURL = FileURL.fromDirectoryURL(directoryURL, fileName);
  await fileURL.create(Aborter.none, content.length);
  console.log(`Create file ${fileName} successfully`);

  // Upload file range
  await fileURL.uploadRange(Aborter.none, content, 0, content.length);
  console.log(`Upload file range "${content}" to ${fileName} successfully`);

  // List directories and files
  console.log(`List directories and files under directory ${directoryName}`);
  marker = undefined;
  do {
    const listFilesAndDirectoriesResponse = await directoryURL.listFilesAndDirectoriesSegment(
      Aborter.none,
      marker
    );

    marker = listFilesAndDirectoriesResponse.nextMarker;
    for (const file of listFilesAndDirectoriesResponse.segment.fileItems) {
      console.log(`\tFile: ${file.name}`);
    }
    for (const directory of listFilesAndDirectoriesResponse.segment
      .directoryItems) {
      console.log(`\tDirectory: ${directory.name}`);
    }
  } while (marker);

  // Get file content from position 0 to the end
  // In Node.js, get downloaded data by accessing downloadFileResponse.readableStreamBody
  // In browsers, get downloaded data by accessing downloadFileResponse.blobBody
  const downloadFileResponse = await fileURL.download(Aborter.none, 0);
  console.log(
    `Downloaded file content${await streamToString(
      downloadFileResponse.readableStreamBody
    )}`
  );

  // Delete share
  await shareURL.delete(Aborter.none);
  console.log(`deleted share ${shareName}`);
}
async function main() {
  // Enter your storage account name and shared key
  const account = "";
  const accountKey = "";

  // Use SharedKeyCredential with storage account and account key
  // SharedKeyCredential is only avaiable in Node.js runtime, not in browsers
  const sharedKeyCredential = new SharedKeyCredential(account, accountKey);

  // Use AnonymousCredential when url already includes a SAS signature
  const anonymousCredential = new AnonymousCredential();

  // Use sharedKeyCredential or anonymousCredential to create a pipeline
  const pipeline = StorageURL.newPipeline(sharedKeyCredential);

  // List shares
  const serviceURL = new ServiceURL(
    // When using AnonymousCredential, following url should include a valid SAS
    `https://${account}.file.core.windows.net`,
    pipeline
  );

  console.log(`List shares`);
  let marker;
  do {
    const listSharesResponse = await serviceURL.listSharesSegment(
      Aborter.none,
      marker
    );

    marker = listSharesResponse.nextMarker;
    for (const share of listSharesResponse.shareItems) {
      console.log(`\tShare: ${share.name}`);
    }
  } while (marker);

  // Create a share
  const shareName = `newshare${new Date().getTime()}`;
  const shareURL = ShareURL.fromServiceURL(serviceURL, shareName);
  await shareURL.create(Aborter.none);
  console.log(`Create share ${shareName} successfully`);

  // Create a directory
  const directoryName = `newdirectory${new Date().getTime()}`;
  const directoryURL = DirectoryURL.fromShareURL(shareURL, directoryName);
  await directoryURL.create(Aborter.none);
  console.log(`Create directory ${directoryName} successfully`);

  // Create a file
  const content = "Hello World!";
  const fileName = "newfile" + new Date().getTime();
  const fileURL = FileURL.fromDirectoryURL(directoryURL, fileName);
  await fileURL.create(Aborter.none, content.length);
  console.log(`Create file ${fileName} successfully`);

  // Upload file range
  await fileURL.uploadRange(Aborter.none, content, 0, content.length);
  console.log(`Upload file range "${content}" to ${fileName} successfully`);

  // List directories and files
  console.log(`List directories and files under directory ${directoryName}`);
  marker = undefined;
  do {
    const listFilesAndDirectoriesResponse = await directoryURL.listFilesAndDirectoriesSegment(
      Aborter.none,
      marker
    );

    marker = listFilesAndDirectoriesResponse.nextMarker;
    for (const file of listFilesAndDirectoriesResponse.segment.fileItems) {
      console.log(`\tFile: ${file.name}`);
    }
    for (const directory of listFilesAndDirectoriesResponse.segment
      .directoryItems) {
      console.log(`\tDirectory: ${directory.name}`);
    }
  } while (marker);

  // Get file content from position 0 to the end
  // In Node.js, get downloaded data by accessing downloadFileResponse.readableStreamBody
  // In browsers, get downloaded data by accessing downloadFileResponse.blobBody
  const downloadFileResponse = await fileURL.download(Aborter.none, 0);
  console.log(
    `Downloaded file content${await streamToString(
      downloadFileResponse.readableStreamBody
    )}`
  );

  // Delete share
  await shareURL.delete(Aborter.none);
  console.log(`deleted share ${shareName}`);
}
async function main ( ) { // Enter your storage account name and shared key const account = "" ; const accountKey = "" ; // Use SharedKeyCredential with storage account and account key // SharedKeyCredential is only avaiable in Node.js runtime, not in browsers const sharedKeyCredential = new SharedKeyCredential ( account , accountKey ) ; // Use AnonymousCredential when url already includes a SAS signature const anonymousCredential = new AnonymousCredential ( ) ; // Use sharedKeyCredential or anonymousCredential to create a pipeline const pipeline = StorageURL . newPipeline ( sharedKeyCredential ) ; // List shares const serviceURL = new ServiceURL ( // When using AnonymousCredential, following url should include a valid SAS ` ${ account } ` , pipeline ) ; console . log ( ` ` ) ; let marker ; do { const listSharesResponse = await serviceURL . listSharesSegment ( Aborter . none , marker ) ; marker = listSharesResponse . nextMarker ; for ( const share of listSharesResponse . shareItems ) { console . log ( ` \t ${ share . name } ` ) ; } } while ( marker ) ; // Create a share const shareName = ` ${ new Date ( ) . getTime ( ) } ` ; const shareURL = ShareURL . fromServiceURL ( serviceURL , shareName ) ; await shareURL . create ( Aborter . none ) ; console . log ( ` ${ shareName } ` ) ; // Create a directory const directoryName = ` ${ new Date ( ) . getTime ( ) } ` ; const directoryURL = DirectoryURL . fromShareURL ( shareURL , directoryName ) ; await directoryURL . create ( Aborter . none ) ; console . log ( ` ${ directoryName } ` ) ; // Create a file const content = "Hello World!" ; const fileName = "newfile" + new Date ( ) . getTime ( ) ; const fileURL = FileURL . fromDirectoryURL ( directoryURL , fileName ) ; await fileURL . create ( Aborter . none , content . length ) ; console . log ( ` ${ fileName } ` ) ; // Upload file range await fileURL . uploadRange ( Aborter . none , content , 0 , content . length ) ; console . log ( ` ${ content } ${ fileName } ` ) ; // List directories and files console . log ( ` ${ directoryName } ` ) ; marker = undefined ; do { const listFilesAndDirectoriesResponse = await directoryURL . listFilesAndDirectoriesSegment ( Aborter . none , marker ) ; marker = listFilesAndDirectoriesResponse . nextMarker ; for ( const file of listFilesAndDirectoriesResponse . segment . fileItems ) { console . log ( ` \t ${ file . name } ` ) ; } for ( const directory of listFilesAndDirectoriesResponse . segment . directoryItems ) { console . log ( ` \t ${ directory . name } ` ) ; } } while ( marker ) ; // Get file content from position 0 to the end // In Node.js, get downloaded data by accessing downloadFileResponse.readableStreamBody // In browsers, get downloaded data by accessing downloadFileResponse.blobBody const downloadFileResponse = await fileURL . download ( Aborter . none , 0 ) ; console . log ( ` ${ await streamToString ( downloadFileResponse . readableStreamBody ) } ` ) ; // Delete share await shareURL . delete ( Aborter . none ) ; console . log ( ` ${ shareName } ` ) ; }
Change to "@azure/storage-file" in your package
Change to