Creating and uploading a temp file

Hey there,

I’m trying to write to a text file using a node script and need to make the file available at a public URL, though only temporarily (streaming the file content over HTTP isn’t going to work for my use case).

Am I able to access the /tmp directory publicly, and if so, how?

Here’s an example snippet of creating a simple file, for context:

import fs from 'fs';

async function run() {
  const filename = '/tmp/myFile.txt';
  await fs.promises.writeFile(filename, Buffer.from("hello, world"));
  console.log(`File has been written to ${filename}`);
}

run();

What I’ll need to be doing ultimately is creating an SRT file with content received from a webhook.

Hi @matt4,

The /tmp directory in Pipedream is not publicly accessible. However, you can achieve your goal by uploading the file to a temporary storage service like Amazon S3 or Google Cloud Storage, and then generate a temporary public URL for the file.

Here’s an example of how you can upload a file to Amazon S3 using the AWS SDK:

import { S3 } from "aws-sdk";
import fs from "fs";

export default defineComponent({
  props: {
    awsAccessKeyId: {
      type: "string",
      label: "AWS Access Key ID",
    },
    awsSecretAccessKey: {
      type: "string",
      label: "AWS Secret Access Key",
    },
    s3Bucket: {
      type: "string",
      label: "S3 Bucket",
    },
  },
  async run({ steps, $ }) {
    const s3 = new S3({
      accessKeyId: this.awsAccessKeyId,
      secretAccessKey: this.awsSecretAccessKey,
    });

    const fileStream = fs.createReadStream("/tmp/myFile.txt");
    const params = {
      Bucket: this.s3Bucket,
      Key: "myFile.txt",
      Body: fileStream,
    };

    await s3.upload(params).promise();

    // Generate a temporary public URL for the file
    const signedUrl = s3.getSignedUrl("getObject", {
      Bucket: this.s3Bucket,
      Key: "myFile.txt",
      Expires: 60 * 5, // URL expires in 5 minutes
    });

    return { signedUrl };
  },
});

Replace the AWS credentials and S3 bucket with your own values. This example uploads the file to S3 and generates a temporary public URL that expires in 5 minutes. You can adjust the expiration time as needed.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

1 Like