Open xlibraries opened 7 months ago
var request = require('request');
var options = {
'method': 'POST',
'url': '[https://l293mx5b2g.execute-api.us-west-2.amazonaws.com/default/post_api'](https://l293mx5b2g.execute-api.us-west-2.amazonaws.com/default/post_api%27),
'headers': {
'Content-Type': 'application/octet-stream'
},
body: "<file contents here>"
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
if required us Postman:- curl --location 'https://l293mx5b2g.execute-api.us-west-2.amazonaws.com/default/post_api' \ --header 'Content-Type: application/octet-stream' \ --data '@/Users/asrivastava22/Downloads/testingfiles.zip'
https://xymdrcfnli.execute-api.us-west-2.amazonaws.com/default/Upload_zip
`import os import boto3 import uuid import zipfile import logging from io import BytesIO import base64 import datetime
logger = logging.getLogger() logger.setLevel(logging.INFO)
s3 = boto3.client('s3')
def extract_zip_to_s3(zip_content, bucket_name, folder_name):
zip_buffer = BytesIO(zip_content)
# Extract the zip file contents
with zipfile.ZipFile(zip_buffer, 'r') as zip_ref:
for file_info in zip_ref.infolist():
# Read the file content
file_content = zip_ref.read(file_info.filename)
# Upload the file content to S3
s3.put_object(Body=file_content, Bucket=bucket_name, Key=f"{folder_name}/{file_info.filename}")
logger.info(f"Uploaded {file_info.filename} to S3 folder: {folder_name}")
def lambda_handler(event, context): try:
bucket_name = 'uploaded-files-semicolon'
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")
# Generate a random UUID for the folder
uuid_generate = str(uuid.uuid4().hex)[:8]
folder_name = f"{timestamp}-{uuid_generate}"
# Read the zip file in binary mode
with open('your_file.zip', 'rb') as file:
zip_content = file.read()
# Encode the zip file content to base64
zip_file_base64 = base64.b64encode(zip_content).decode('utf-8')
# Extract the zip file contents and upload them to S3
zip_content = zip_file_base64.encode('utf-8')
print('event', zip_content)
zip_file_bytes = base64.b64decode(zip_file_base64)
print('zip_file_bytes', zip_file_bytes)
extract_zip_to_s3(zip_file_bytes, bucket_name, folder_name)
# Return a success response
return {
'statusCode': 200,
'body': f'Zip file extracted and files uploaded successfully to S3 in folder: {folder_name}'
}
except Exception as e:
# Log the error
logger.error(f"Error: {str(e)}")
# Return an error response
return {
'statusCode': 500,
'body': 'Internal server error'
}
`
testing-zip-upload
import React, { useEffect, useState } from 'react';
import json1 from './json1.json'; // path to your json1 file
import json2 from './json2.json'; // path to your json2 file
const YourComponent = () => {
const [data, setData] = useState(null);
const [flag, setFlag] = useState(true); // set this state based on your conditions
useEffect(() => {
// Sleep for 20 seconds
new Promise((resolve) => setTimeout(resolve, 20000))
.then(() => {
// After 20 seconds, decide which JSON to use based on the flag
if (flag) {
setData(json1);
} else {
setData(json2);
}
});
}, [flag]); // This effect runs whenever the flag changes
// Now you can use data
in your render method
return (
);
};
export default YourComponent;
Functional impact: None