1
0
Fork 0
mirror of synced 2024-10-01 01:28:51 +13:00

Add public URL of uploaded file to button action outputs when uploading files to S3

This commit is contained in:
Andrew Kingston 2022-01-14 10:40:38 +00:00
parent 5f357ad2d8
commit 157b6a19f6
7 changed files with 31 additions and 12 deletions

View file

@ -73,7 +73,13 @@
}, },
{ {
"name": "Upload File to S3", "name": "Upload File to S3",
"component": "S3Upload" "component": "S3Upload",
"context": [
{
"label": "File URL",
"value": "publicUrl"
}
]
} }
] ]
} }

View file

@ -25,14 +25,18 @@ export const getSignedDatasourceURL = async (datasourceId, bucket, key) => {
if (res.error) { if (res.error) {
throw "Could not generate signed upload URL" throw "Could not generate signed upload URL"
} }
return res?.signedUrl return res
} }
/** /**
* Uploads a file to an external datasource. * Uploads a file to an external datasource.
*/ */
export const externalUpload = async (datasourceId, bucket, key, data) => { export const externalUpload = async (datasourceId, bucket, key, data) => {
const signedUrl = await getSignedDatasourceURL(datasourceId, bucket, key) const { signedUrl, publicUrl } = await getSignedDatasourceURL(
datasourceId,
bucket,
key
)
const res = await API.put({ const res = await API.put({
url: signedUrl, url: signedUrl,
body: data, body: data,
@ -42,4 +46,5 @@ export const externalUpload = async (datasourceId, bucket, key, data) => {
if (res?.error) { if (res?.error) {
throw "Could not upload file to signed URL" throw "Could not upload file to signed URL"
} }
return { publicUrl }
} }

View file

@ -71,12 +71,13 @@
const upload = async () => { const upload = async () => {
loading = true loading = true
try { try {
await API.externalUpload(datasourceId, bucket, key, data) const res = await API.externalUpload(datasourceId, bucket, key, data)
notificationStore.actions.success("File uploaded successfully") notificationStore.actions.success("File uploaded successfully")
loading = false
return res
} catch (error) { } catch (error) {
notificationStore.actions.error(`Error uploading file: ${error}`) notificationStore.actions.error(`Error uploading file: ${error}`)
} }
loading = false
} }
onMount(() => { onMount(() => {

View file

@ -30,7 +30,7 @@ export const createUploadStore = () => {
} }
const component = get(store).find(c => c.componentId === componentId) const component = get(store).find(c => c.componentId === componentId)
await component?.callback() return await component?.callback()
} }
return { return {

View file

@ -163,7 +163,10 @@ const s3UploadHandler = async action => {
if (!componentId) { if (!componentId) {
return return
} }
await uploadStore.actions.processFileUpload(componentId) const res = await uploadStore.actions.processFileUpload(componentId)
return {
publicUrl: res?.publicUrl,
}
} }
const handlerMap = { const handlerMap = {

View file

@ -124,6 +124,7 @@ exports.getSignedUploadURL = async function (ctx) {
// Determine type of datasource and generate signed URL // Determine type of datasource and generate signed URL
let signedUrl let signedUrl
let publicUrl
if (datasource.source === "S3") { if (datasource.source === "S3") {
const { bucket, key } = ctx.request.body || {} const { bucket, key } = ctx.request.body || {}
if (!bucket || !key) { if (!bucket || !key) {
@ -140,10 +141,11 @@ exports.getSignedUploadURL = async function (ctx) {
}) })
const params = { Bucket: bucket, Key: key } const params = { Bucket: bucket, Key: key }
signedUrl = s3.getSignedUrl("putObject", params) signedUrl = s3.getSignedUrl("putObject", params)
publicUrl = `https://${bucket}.s3.${AWS_REGION}.amazonaws.com/${key}`
} catch (error) { } catch (error) {
ctx.throw(400, error) ctx.throw(400, error)
} }
} }
ctx.body = { signedUrl } ctx.body = { signedUrl, publicUrl }
} }

View file

@ -41,16 +41,18 @@ describe("/attachments", () => {
}) })
it("should be able to generate a signed upload URL", async () => { it("should be able to generate a signed upload URL", async () => {
const bucket = "foo"
const key = "bar"
const res = await request const res = await request
.post(`/api/attachments/${datasource._id}/url`) .post(`/api/attachments/${datasource._id}/url`)
.send({ .send({ bucket, key })
bucket: "foo",
key: "bar",
})
.set(config.defaultHeaders()) .set(config.defaultHeaders())
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
expect(res.body.signedUrl).toEqual("my-url") expect(res.body.signedUrl).toEqual("my-url")
expect(res.body.publicUrl).toEqual(
`https://${bucket}.s3.eu-west-1.amazonaws.com/${key}`
)
}) })
it("should handle an invalid datasource ID", async () => { it("should handle an invalid datasource ID", async () => {