Best JavaScript code snippet using argos
bucketActions.js
Source:bucketActions.js
1'use strict';2var Promise = require('promise');3var AWS = require('aws-sdk-promise');4var Hoek = require('hoek');5var internals = {};6internals.s3 = new AWS.S3();7internals.defaultS3Params = {8 Bucket: process.env.AWS_S3_BUCKET9};10internals.downloadNameAlreadyExistsError = function() {11 throw new Error('Download already exists in the system!');12};13internals.defaultError = function(err) {14 console.log(err);15 throw err;16};17exports.validateSettings = internals.validateSettings = function() {18 return internals.s3.headBucket(internals.defaultS3Params).promise();19};20exports.createDownload = internals.createDownload = function(downloadName) {21 return internals.doesDownloadExist(downloadName).then(function(22 downloadExists) {23 if (downloadExists === true) {24 internals.downloadNameAlreadyExistsError();25 } else {26 return internals.putEmptyObject(downloadName);27 }28 });29};30exports.doesDownloadExist = internals.doesDownloadExist = function(downloadName) {31 return internals.headObject(downloadName)32 .then(function() {33 return true;34 },35 function(err) {36 // If the error is that the object does not exist37 if (err.code === 'NotFound') {38 // Add the object39 return false;40 } else {41 // If the error is some other problem, throw it.42 internals.defaultError(err);43 }44 });45};46exports.putEmptyObject = internals.putEmptyObject = function(objectName) {47 var s3Params = Hoek.applyToDefaults(internals.defaultS3Params, {48 Key: objectName49 });50 return internals.s3.putObject(s3Params).promise();51};52exports.putTextObject = internals.putTextObject = function(objectName, acl, data, author) {53 var s3Params = Hoek.applyToDefaults(internals.defaultS3Params, {54 Key: objectName,55 ACL: acl,56 Body: data,57 Metadata: {58 Author: author59 }60 });61 return internals.s3.putObject(s3Params).promise();62};63exports.waitFor = internals.waitFor = function(event, downloadName) {64 var s3Params = Hoek.applyToDefaults(internals.defaultS3Params, {65 Key: downloadName66 });67 return internals.s3.waitFor(event, s3Params).promise();68};69exports.headObject = internals.headObject = function(downloadName) {70 var s3Params = Hoek.applyToDefaults(internals.defaultS3Params, {71 Key: downloadName72 });73 return internals.s3.headObject(s3Params).promise();74};75exports.listBucketDirectories = internals.listBucketDirectories = function() {76 return internals.listBucket({77 Delimiter: '/'78 }).then(function(response) {79 return response.data.CommonPrefixes;80 });81};82exports.listFiles = internals.listFiles = function(downloadName) {83 return internals.listBucket({84 Prefix: downloadName85 }).then(function(response) {86 var returnData = response.data.Contents || [];87 response.data.CommonPrefixes.forEach(function(directory) {88 returnData.push({89 Key: directory.Prefix,90 LastModified: null,91 IsDirectory: true92 });93 });94 return returnData;95 });96};97exports.listBucket = internals.listBucket = function(params) {98 var s3Params = Hoek.applyToDefaults(internals.defaultS3Params, params);99 return internals.s3.listObjects(s3Params).promise();100};101exports.deleteObjects = internals.deleteObjects = function(params) {102 var s3Params = Hoek.applyToDefaults(internals.defaultS3Params, params);103 return internals.s3.deleteObjects(s3Params).promise();104};105exports.deleteObject = internals.deleteObject = function(downloadName) {106 var s3Params = Hoek.applyToDefaults(internals.defaultS3Params, {107 Key: downloadName108 });109 return internals.s3.deleteObject(s3Params).promise();110};111exports.getSignedPutObjectUrl = internals.getSignedPutObjectUrl = function(112 downloadName, contentType, author) {113 var s3Params = {114 Key: downloadName,115 ContentType: contentType,116 Expires: 60,117 Metadata: {118 Author: author119 }120 };121 return internals.getSignedUrl('putObject', s3Params);122};123exports.getSignedGetObjectUrl = internals.getSignedGetObjectUrl = function(124 downloadName) {125 var s3Params = {126 Key: downloadName,127 Expires: 60128 };129 return internals.getSignedUrl('getObject', s3Params);130};131internals.getSignedUrl = function(operation, s3Params) {132 return new Promise(function(accept, reject) {133 var newS3Params = Hoek.applyToDefaults(internals.defaultS3Params,134 s3Params);135 // aws-sdk-promise does not work on this function because getSignedUrl 136 // specifies a "synchronous" version that takes in two parameters.137 internals.s3.getSignedUrl(operation, newS3Params, function(138 err, url) {139 if (err) {140 reject(err);141 } else {142 accept(url);143 }144 });145 });...
FileResolver.js
Source:FileResolver.js
1import { UserInputError } from 'apollo-server';2import * as Yup from 'yup';3import uuidv4 from 'uuid';4import { S3 } from '../../config/aws';5async function getSignedPutObjectUrl(objectId, contentType) {6 return S3.getSignedUrlPromise('putObject', {7 Bucket: 'mybucket',8 Key: objectId,9 Expires: 15 * 60,10 ContentType: contentType,11 ACL: 'public-read',12 });13}14export default async function requestUpload(context, { input }) {15 const schema = Yup.object().shape({16 fileName: Yup.string().required(),17 fileType: Yup.string().required(),18 contentType: Yup.string().required(),19 });20 await schema.validate(input).catch((err) => {21 throw new UserInputError(err.name, { ...err.errors });22 });23 const userContext = context.user;24 const { fileName, fileType, contentType } = input;25 const { sub } = userContext;26 const uuid = uuidv4();27 const objectId = `${fileType.toLowerCase()}/${sub}/${uuid}/${fileName.toLowerCase()}`;28 const url = await getSignedPutObjectUrl(objectId, contentType);29 return { url, objectId };30}31// async function getSignedGetObjectUrl(objectId) {32// return S3.getSignedUrlPromise('getObject', {33// Bucket: 'gatamalvada',34// Key: objectId,35// Expires: 100,36// });...
signed-url.test.js
Source:signed-url.test.js
...9 beforeEach(() => {10 s3 = new S3Client({ region: "eu-west-1" });11 });12 it("generate a signed URL used to upload", async () => {13 const url = await getSignedPutObjectUrl({14 s3,15 Bucket: config.get("s3.screenshotsBucket"),16 Key: "test2.png",17 ChecksumSHA256: "test",18 });19 const inputPath = path.join(20 __dirname,21 "__fixtures__",22 "screenshot_test.jpg"23 );24 const file = await fs.readFile(inputPath);25 const axiosResponse = await axios({26 method: "PUT",27 url,...
Using AI Code Generation
1const argos = require('argos-sdk');2argos.getSignedPutObjectUrl('PUT_OBJECT_URL', 'FILE_NAME', 'FILE_TYPE')3.then(putObjectUrl => {4})5const argos = require('argos-sdk');6argos.getSignedGetObjectUrl('GET_OBJECT_URL')7.then(getObjectUrl => {8})9const argos = require('argos-sdk');10argos.getSignedDeleteObjectUrl('DELETE_OBJECT_URL')11.then(deleteObjectUrl => {12})13const argos = require('argos-sdk');14argos.getSignedPostObjectUrl('POST_OBJECT_URL', 'FILE_NAME', 'FILE_TYPE')15.then(postObjectUrl => {16})17const argos = require('argos-sdk');18argos.getSignedPutObjectUrl('PUT_OBJECT_URL', 'FILE_NAME', 'FILE_TYPE')19.then(putObjectUrl => {20})21const argos = require('argos-sdk');22argos.getSignedGetObjectUrl('GET_OBJECT_URL')23.then(getObjectUrl => {24})25const argos = require('argos-sdk');26argos.getSignedDeleteObjectUrl('DELETE_OBJECT_URL')27.then(deleteObjectUrl => {28})29const argos = require('argos-sdk');30argos.getSignedPostObjectUrl('POST_OBJECT_URL', 'FILE_NAME', 'FILE_TYPE')31.then(postObjectUrl => {32})
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!