App Builder Files SDK
Return a list of files stored with the Files SDK
Copied to your clipboard
const { Core, Files } = require('@adobe/aio-sdk')
const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../../utils')
async function main (params) {
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })
try {
logger.info('Calling the main action')
logger.debug(stringParameters(params))
const requiredParams = ['path']
const requiredHeaders = []
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
if (errorMessage) {
return errorResponse(400, errorMessage, logger)
}
const files = await Files.init()
const fileList = await files.list(params.path)
logger.debug(fileList)
const response = {
headers: {},
body: {
fileList
},
statusCode: 200
}
logger.info(`${response.statusCode}: successful request`)
return response
} catch (error) {
logger.error(error)
return errorResponse(500, 'server error', logger)
}
}
exports.main = main
Return a presigned URL of a private file from the Files SDK
Copied to your clipboard
const { Core, Files } = require('@adobe/aio-sdk')
const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../../utils')
async function main (params) {
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })
try {
logger.info('Calling the main action')
logger.debug(stringParameters(params))
const requiredParams = ['fileLocation']
const requiredHeaders = []
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
if (errorMessage) {
return errorResponse(400, errorMessage, logger)
}
const files = await Files.init()
const presignUrl = await files.generatePresignURL(params.fileLocation, { expiryInSeconds: 60 })
logger.debug(presignUrl)
const response = {
headers: {},
body: {
presignUrl
},
statusCode: 200
}
logger.info(`${response.statusCode}: successful request`)
return response
} catch (error) {
logger.error(error)
return errorResponse(500, 'server error', logger)
}
}
exports.main = main
Return a file from the Files SDK with Redirect
Copied to your clipboard
const { Core, Files } = require('@adobe/aio-sdk')
const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../../utils')
async function main (params) {
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })
try {
logger.info('Calling the main action')
logger.debug(stringParameters(params))
const requiredParams = ['fileLocation']
const requiredHeaders = []
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
if (errorMessage) {
return errorResponse(400, errorMessage, logger)
}
const files = await Files.init()
const presignUrl = await files.generatePresignURL(params.fileLocation, { expiryInSeconds: 60 })
logger.debug(presignUrl)
const response = {
headers: { location: presignUrl },
statusCode: 302
}
logger.info(`${response.statusCode}: successful request`)
return response
} catch (error) {
logger.error(error)
return errorResponse(500, 'server error', logger)
}
}
exports.main = main
Upload a file to the Files SDK
Copied to your clipboard
const { Core, Files } = require('@adobe/aio-sdk')
const fetch = require('node-fetch')
const { errorResponse, getBearerToken, stringParameters, checkMissingRequestInputs } = require('../../utils')
async function main (params) {
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })
try {
logger.info('Calling the main action')
logger.debug(stringParameters(params))
const requiredParams = ['fileUrl', 'fileLocation']
const requiredHeaders = []
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
if (errorMessage) {
return errorResponse(400, errorMessage, logger)
}
const originalFile = await fetch(params.fileUrl)
const files = await Files.init()
logger.info('File SDK init done')
const fileLocation = params.fileLocation
await files.write(fileLocation, originalFile.body)
const props = await files.getProperties(fileLocation)
logger.debug(JSON.stringify(props))
const response = {
statusCode: 200,
body: {}
}
logger.info(`${response.statusCode}: successful request`)
return response
} catch (error) {
logger.error(error)
return errorResponse(500, 'server error', logger)
}
}
exports.main = main