Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"@google-cloud/storage" npm package tracking issue #18730

Closed
Tracked by #18836
bartlomieju opened this issue Apr 16, 2023 · 1 comment
Closed
Tracked by #18836

"@google-cloud/storage" npm package tracking issue #18730

bartlomieju opened this issue Apr 16, 2023 · 1 comment
Labels
node API Related to various "node:*" modules APIs node compat

Comments

@bartlomieju
Copy link
Member

https://www.npmjs.com/package/@google-cloud/storage

@satyarohith
Copy link
Member

I tested 4 operations: create/delete bucket, create/delete object and they work. You can try the following example if you're interested.

import { Storage } from '@google-cloud/storage'; // Install the package using deno add npm:@google-cloud/storage
import process from 'node:process';

// Configure project ID (replace with yours)
const projectId = 'PROJECT_ID';

// Create a Storage client
const storage = new Storage({ projectId });

// Async function for creating a bucket
async function createBucket(bucketName) {
  try {
    await storage.createBucket(bucketName);
    console.log(`Bucket ${bucketName} created successfully.`);
  } catch (error) {
    console.error(`Error creating bucket: ${error.message}`);
  }
}

// Async function for deleting a bucket
async function deleteBucket(bucketName) {
  try {
    await storage.bucket(bucketName).delete();
    console.log(`Bucket ${bucketName} deleted successfully.`);
  } catch (error) {
    console.error(`Error deleting bucket: ${error.message}`);
  }
}

// Async function for uploading a file
async function uploadFile(bucketName, fileName, filePath) {
  try {
    await storage.bucket(bucketName).upload(filePath, { destination: fileName });
    console.log(`File ${fileName} uploaded to bucket ${bucketName}`);
  } catch (error) {
    console.error(`Error uploading file: ${error.message}`);
  }
}

// Async function for deleting a file
async function deleteFile(bucketName, fileName) {
  try {
    await storage.bucket(bucketName).file(fileName).delete();
    console.log(`File ${fileName} deleted from bucket ${bucketName}`);
  } catch (error) {
    console.error(`Error deleting file: ${error.message}`);
  }
}

// Command line arguments handling
const [, , command, bucketName, fileName, filePath] = process.argv;

// Main function based on command
async function main() {
  switch (command) {
    case 'create-bucket':
      if (bucketName) {
        await createBucket(bucketName);
      } else {
        console.error('Please provide a bucket name.');
      }
      break;
    case 'delete-bucket':
      if (bucketName) {
        await deleteBucket(bucketName);
      } else {
        console.error('Please provide a bucket name.');
      }
      break;
    case 'upload-file':
      if (bucketName && fileName && filePath) {
        await uploadFile(bucketName, fileName, filePath);
      } else {
        console.error('Please provide bucket name, file name, and file path.');
      }
      break;
    case 'delete-file':
      if (bucketName && fileName) {
        await deleteFile(bucketName, fileName);
      } else {
        console.error('Please provide bucket name and file name.');
      }
      break;
    default:
      console.error('Invalid command. Available commands: create-bucket, delete-bucket, upload-file, delete-file');
  }
}

main().catch((error) => {
  console.error('An unexpected error occurred:', error);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node API Related to various "node:*" modules APIs node compat
Projects
None yet
Development

No branches or pull requests

4 participants