Need help working with files in plugins

Hi everyone!

I have a plugin that I’ve written for interacting with AWS S3 via their node library. It works well so far, but I’m having issues adapting one function to allow file upload.

My workflow is that a user uploads a file via the Bubble UI using a file uploader and then the client passes the selected file to a backend workflow which in turns calls my plugin with the following code:

async function(properties, context) {
    
    /**
     * For details on the PutObjectCommand, see:
     * https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/command/PutObjectCommand
     */
    const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
    
    // Create the S3Client instance
    const s3Client = new S3Client({
        region: properties.aws_region,
        credentials: {
            accessKeyId: properties.user_access_key,
            secretAccessKey: properties.user_secret_key
        }
    });
    
    // Create the call input
    let input = {
        Bucket: properties.bucket,
        Key: properties.object_key,
    };
    
    // Add file information, if provided
    if (properties.file) {
        
        // Get the file from Bubble's S3 bucket
        const options = {
            uri: "https:" + properties.file,
            method: "GET",
            headers: {
                "content-type": "application/octet-stream"
            }
        }
        const fileStream = context.request(options).body;
        
        input['Body'] = fileStream;
        input['ContentType'] = properties.file.type;
    }
    
    var response = '';
    try {
        // Create the command
        const command = new PutObjectCommand(input);
        response = await s3Client.send(command);
    } catch (error) {
        return ({
            error: `Something went wrong: ${error.message}`
        });
    }
    
    // Return the success response
    return ({
        expiration: response.Expiration,
        version_id: response.VersionId
    });
}

I’m operating under the assumption that the “file” property passed in is just the file’s URL (even though it is of type “file” in the action definition).

The initial problem I was seeing is that a new object key (e.g. path) was being created in S3 in the correct location but it was empty, so the file content wasn’t really being uploaded.

Now I’m trying to read the file from Bubble’s file path and then write it back out to my S3 bucket and path. I’m trying to use context.request() for that, but I get the error “TypeError: context.request is not a function”.

I’m not quite sure where I’m going wrong.

Thanks!

I figured it out!

For anyone else that comes across this issue, my problem was that the plugin docs reference the old way of using context.request and context.async. Both of those have been updated since the v4 update. Now they use async context.v3.async and async context.v3.request.

Now if Bubble would just update their docs…

See here for more details: https://manual.bubble.io/account-and-marketplace/building-plugins/updating-to-plugin-api-v4

This topic was automatically closed after 70 days. New replies are no longer allowed.