#Module export file from buffer content

1 messages · Page 1 of 1 (latest)

rocky osprey
#

Hi everyone,

I am experimenting/creating a module and i am having problems with something

The function gets an object from a S3 bucket, and i want to save that file on the host

func (m *Module) Download(ctx context.Context, 
// AWS access key
    accessKey *Secret,
    // AWS secret key
    secretKey *Secret,
) (*File, error) {
    // Get bucket obj from S3 code ...

    // Copy obj data to buffer
    var buf bytes.Buffer
    if _, err := io.Copy(&buf, obj.Body); err != nil {
        fmt.Fprintln(os.Stderr, "Error reading file:", err)
        return nil, err
    }

    // Response file,  use buf variable
    return dag.Container().From("alpine:latest").
         WithExec([]string{"touch", "export.txt"}).
         File("export.txt")

}

Is possible to return a File type from a buffer variable? i tried use a dag.File in a container but not sure if i am in the right way.
I call the function in this way

dagger call download --access-key env:AWS_ACCESS_KEY --secret-key env:AWS_SECRET_KEY  export --path=export.txt
leaden swallow
#

Hey @rocky osprey! You are in the right direction! To make it work you can call WithNewFile sending the contents of the buffer and then return that file:

    // Response file,  use buf variable
    return dag.Container().From("alpine:latest").
         WithNewFile("/export.txt", ContainerWithNewFileOpts{Contents: buf.String()}).
         File("/export.txt"), nil