How to list files in amazon s3 bucket with in the folder using knox and nodejs - node.js

I want to list file in my s3 bucket with in the folder, my bucket structure is
/> folder_1
a_bucket -> folder_2
\> folder_3
I want list files in folder_1 only

My problem got solved
s3client.list({ "prefix": "folder_1/" + filePrefix }, function (err, data) {
/* `data` will look roughly like:
{
Prefix: 'my-prefix',
IsTruncated: true,
MaxKeys: 1000,
Contents: [
{
Key: 'whatever'
LastModified: new Date(2012, 11, 25, 0, 0, 0),
ETag: 'whatever',
Size: 123,
Owner: 'you',
StorageClass: 'whatever'
},
⋮
]
}
*/
}

Related

How can I bundle additional files with `NodejsFunction`?

I would like to upload an additional Html file to the code source like below.
this is my code:
const mailerFunction = new aws_lambda_nodejs.NodejsFunction(this, 'ApiNotificationHandler', {
runtime: lambda.Runtime.NODEJS_14_X,
memorySize: 1024,
timeout: cdk.Duration.seconds(3),
handler: 'main',
entry: path.join(__dirname, '../../src/mailer/index.ts'),
environment: {
SES_REGION,
SES_EMAIL_FROM,
SES_EMAIL_TO,
}
});
I use a CDK 2.58.1 version.
How could I upload additional html file to code source with cdk lambda?
You can try to use command hooks.
In Your example it would probably look like this (adjust the inputDir command):
const mailerFunction = new aws_lambda_nodejs.NodejsFunction(this, 'ApiNotificationHandler', {
bundling: {
commandHooks: {
beforeBundling(inputDir: string, outputDir: string): string[] {
return [`cp -r ${inputDir} ${outputDir}`] //adjust here
},
afterBundling(inputDir: string, outputDir: string): string[] {
return []
},
beforeInstall(inputDir: string, outputDir: string): string[] {
return []
},
},
},
runtime: lambda.Runtime.NODEJS_14_X,
memorySize: 1024,
timeout: cdk.Duration.seconds(3),
handler: 'main',
entry: path.join(__dirname, '../../src/mailer/index.ts'),
environment: {
SES_REGION,
SES_EMAIL_FROM,
SES_EMAIL_TO,
}
});
Copy the .html file by defining a commandHook in the bundling prop:
new NodejsFunction(this, "ApiNotificationHandler", {
bundling: {
commandHooks: {
afterBundling: (inputDir: string, outputDir: string): string[] => [
`cp ${inputDir}/path/from/root/to/email-template.html ${outputDir}`,
],
beforeBundling: (inputDir: string, outputDir: string): string[] => [],
beforeInstall: (inputDir: string, outputDir: string): string[] => [],
},
},
// ...
});
The interface requires all three hooks to be defined. Choose one to implement the copying. Return an empty array as a no-op for the other two. inputDir will be the project root directory.

graphql-upload multiple file transfer with apollo-server-express

My use Apollo-server, expressjs;
while there is no problem with small file transfers,
I can't get files after the 1st in nodejs in large files
server.js:
app.use('/graphql',graphqlUploadExpress({ maxFileSize: 100000000, maxFiles: 10 }))
my-resolver:
multiUpload: async (obj, { files },{ Models }) => {
const results = await (files.map(async val=>await val.file));
})
small files resolvers req:
a.txt: 253 bayt
b.txt: 335 bayt
[
Promise {
{
filename: 'a.txt',
mimetype: 'application/octet-stream',
encoding: '7bit',
createReadStream: [Function: createReadStream]
}
},
Promise {
{
filename: 'b.txt',
mimetype: 'text/plain',
encoding: '7bit',
createReadStream: [Function: createReadStream]
}
}
]
big files resolvers req:
a.jpeg: 3.9 Mb
b.jpeg: 4 Mb
Promise {
{
filename: 'a.jpg',
mimetype: 'image/jpeg',
encoding: '7bit',
createReadStream: [Function: createReadStream]
}
},
Promise { undefined }
]
May I know why?
thank you for helping

Apollo GraphQL Server - Access query params from cache plugin

I have an Apollo GraphQL server using the apollo-server-plugin-response-cache plugin and I need to determine whether or not I'm going to write to the cache based on incoming parameters. I have the plugin set up and I'm using the shouldWriteToCache hook. I can print out the GraphQLRequestContext object that gets passed into the hook, and I can see the full request source, but request.variables is empty. Other than parsing the query itself, how can I access the actual params for the resolver in this hook? (In the example below, I need the value of param2.)
Apollo Server:
new ApolloServer({
introspection: true,
playground: true,
subscriptions: false,
typeDefs,
resolvers,
cacheControl: {
defaultMaxAge: 60
},
plugins: [
apolloServerPluginResponseCache({
cache, // This is a "apollo-server-cache-redis" instance
shouldWriteToCache: (requestContext) => {
// I get a lot of info here, including the source query, but not the
// parsed out query variables
console.log(requestContext.request);
// What I want to do here is:
return !context.request.variables.param2
// but `variables` is empty, and I can't see that value parsed anywhere else
}
})
]
})
Here is my resolver:
export async function exapi(variables, context) {
// in here I use context.param1 and context.param2
// ...
}
I have also tried:
export async function exapi(variables, { param1, param2 }) {
// ...
}
Here is what I get logged out from the code above:
{
query: '{\n' +
' exapi(param1: "value1", param2: true) {\n' +
' records\n' +
' }\n' +
'}\n',
operationName: null,
variables: {}, // <-- this is empty?! How can I get param2's value??
extensions: undefined,
http: Request {
size: 0,
timeout: 0,
follow: 20,
compress: true,
counter: 0,
agent: undefined,
[Symbol(Body internals)]: { body: null, disturbed: false, error: null },
[Symbol(Request internals)]: {
method: 'POST',
redirect: 'follow',
headers: [Headers],
parsedURL: [Url],
signal: null
}
}
}
If you didn't provide variables for GraphQL query, you could get the arguments from the GraphQL query string via ArgumentNode of AST
If you provide variables for GraphQL query, you will get them from requestContext.request.variables.
E.g.
server.js:
import apolloServerPluginResponseCache from 'apollo-server-plugin-response-cache';
import { ApolloServer, gql } from 'apollo-server';
import { RedisCache } from 'apollo-server-cache-redis';
const typeDefs = gql`
type Query {
exapi(param1: String, param2: Boolean): String
}
`;
const resolvers = {
Query: {
exapi: (_, { param1, param2 }) => 'teresa teng',
},
};
const cache = new RedisCache({ host: 'localhost', port: 6379 });
const server = new ApolloServer({
introspection: true,
playground: true,
subscriptions: false,
typeDefs,
resolvers,
cacheControl: {
defaultMaxAge: 60,
},
plugins: [
apolloServerPluginResponseCache({
cache,
shouldWriteToCache: (requestContext) => {
console.log(requestContext.document.definitions[0].selectionSet.selections[0].arguments);
return true;
},
}),
],
});
server.listen().then(({ url }) => console.log(`🚀 Server ready at ${url}`));
GraphQL query:
query{
exapi(param1: "value1", param2: true)
}
Server logs print param1 and param2 arguments:
🚀 Server ready at http://localhost:4000/
[]
[ { kind: 'Argument',
name: { kind: 'Name', value: 'param1', loc: [Object] },
value:
{ kind: 'StringValue',
value: 'value1',
block: false,
loc: [Object] },
loc: { start: 15, end: 31 } },
{ kind: 'Argument',
name: { kind: 'Name', value: 'param2', loc: [Object] },
value: { kind: 'BooleanValue', value: true, loc: [Object] },
loc: { start: 33, end: 45 } } ]

AWS Rekognition Comparing Faces with images in folders

I can compare two images if they are located in the root of the S3 bucket.
const params = {
SourceImage: {
S3Object: {
Bucket: bucket,
Name: 'source.jpg'
}
},
TargetImage: {
S3Object: {
Bucket: bucket,
Name: 'target.jpg'
}
},
SimilarityThreshold: 90
}
But I get an error if they are in sub-folders:
message: 'Request has invalid parameters',
code: 'InvalidParameterException',
time: 2019-11-25T13:12:44.498Z,
requestId: '7ac7f297-fc36-436b-a1dc-113d419da766',
statusCode: 400,
retryable: false, retryDelay: 71.0571139838835
If i try to compare images in sub-folders (note I tried with './', '/' before the path - same thing)
const params = {
SourceImage: {
S3Object: {
Bucket: bucket,
Name: '/sub1/sub2/source.jpg'
}
},
TargetImage: {
S3Object: {
Bucket: bucket,
Name: '/sub1/sub2/target.jpg'
}
},
SimilarityThreshold: 90
}
I really need the photos to be in sub-folders. Any help would be appreciated.
Here's a working example.
import boto3
reko=boto3.client('rekognition')
resp = reko.compare_faces(
SourceImage={
'S3Object': {
'Bucket': 'jsimon-public-us',
'Name': 'pref1/image1.jpg',
}},
TargetImage={
'S3Object': {
'Bucket': 'jsimon-public-us',
'Name': 'pref2/image2.jpg',
}}
)
print(resp)

What am I missing here? Node, Express, req.files['image0']

I'm trying to pull the data out of the req.files array but the dot notation isn't working and I don't understand what I'm missing.
My Pug set Up:
if product.image
.col-lg-1.pull-left
img.product-img(src='/images/' + product.image, alt=product.title)
.col-lg-11.pull-right
input(type="hidden", name="mainImg", value=product.image)
input.form-control(type="file", name="image0", id="image0")
.clear
else
.col-lg-11.pull-right
input.form-control(type="file", name="image0", id="image0" value="")
This returns the req.files array as expected:
console.log(req.files)
result:
{ image0:
[ { fieldname: 'image0',
originalname:'Some_image.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'images',
filename: 'bdad727257698479d84157ad0211b05f.jpg',
path: 'images\\bdad727257698479d84157ad0211b05f.jpg',
size: 109205 } ],
image1:
[ { fieldname: 'image1',
originalname:'Some_image.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'images',
filename: '9546c3810ad9d6c471fde641a30078fd.jpg',
path: 'images\\9546c3810ad9d6c471fde641a30078fd.jpg',
size: 109205 } ],
image2:
[ { fieldname: 'image2',
originalname:'Some_image.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'images',
filename: '0bd6d9da3b53348f5b9145723d4da340.jpg',
path: 'images\\0bd6d9da3b53348f5b9145723d4da340.jpg',
size: 78420 } ]
} '#################'
This returns the first array element as expected:
console.log(req.files['image0'], '#################')
result:
[ { fieldname: 'image0',
originalname: 'Some_Image_Name.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'images',
filename: '28efd1e7544820acc0ac174ac8404ea9.jpg',
path: 'images\\28efd1e7544820acc0ac174ac8404ea9.jpg',
size: 109205 } ] '#################'
This returns undefined and I don't understand why:
console.log(req.files['image0'].fieldname)
Here is my multer setup:
const upload = multer({dest: 'images', storage: imgStorage, fileFilter: fileFilter });
// app.use(upload.array('image'));
app.use(upload.fields([
{
name: 'image0', maxCount: 1
},
{
name: 'image1', maxCount: 1
},
{
name: 'image2', maxCount: 1
},
{
name: 'image3', maxCount: 1
}
])
I eventually want to set up a loop to iterate through a possible 20 or more pictures but I want to get the data first before I try to tackle a loop like that. This project is for a multi-image upload where the images are not overwritten if there is no image data passed and if data is passed only the image specified is overwritten. That is why I'm using upload.fields instead of .any() or array().
Any help or advice would be greatly appreciated.
You are trying to access a property of an array as if it's an object. This is invalid Javascript.
Try: req.files['image0'][0].fieldname
req.files['image0'] returns:
[ { fieldname: 'image0',
originalname: 'Some_Image_Name.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'images',
filename: '28efd1e7544820acc0ac174ac8404ea9.jpg',
path: 'images\\28efd1e7544820acc0ac174ac8404ea9.jpg',
size: 109205 } ]
Which is an array.
req.files['image0'][0] returns:
{ fieldname: 'image0',
originalname: 'Some_Image_Name.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'images',
filename: '28efd1e7544820acc0ac174ac8404ea9.jpg',
path: 'images\\28efd1e7544820acc0ac174ac8404ea9.jpg',
size: 109205 }
Which is an object.
You can access the property fieldname from this object.

Resources