Question

In: Computer Science

PLEASE WRITE CLEAR AND CONCISE CODE Subject is Unix system programming Write code that downloads all...

PLEASE WRITE CLEAR AND CONCISE CODE

Subject is Unix system programming

Write code that downloads all of the log files ending in `.log` in an S3 bucket and counts the total number of HTTP errors in those logs.

Log lines are in the format `{"path": "/", status: 200}` or `{"path": "/", status: 404}`, for example. (This is JSON, and you can process it as such if you choose.)

Use the bucket `class6-logs`, the access key ID "AKIASUMBPHIPY6DLZ4C5", and the secret access key "JQdQIbxsRcipnoKFnsfse44SMRGouzz4tbAzTYbe".

Use the code below  to get started:

Code-

import boto3

client = boto3.client(

's3',

aws_access_key_id="AKIASUMBPHIPY6DLZ4C5",

aws_secret_access_key="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",

)

resp = client.list_objects(Bucket='class6-logs')

client.download_file('class6-logs', resp['Contents'][0]

['Key'], 'downloaded_file')

# for object in resp['Contents']:

# print(object['Key'])

# print(resp['Contents'][0]['Key'])

Solutions

Expert Solution

import boto3
import os

def download_dir(client, resource, dist, local='/tmp', bucket='your_bucket'):
    paginator = client.get_paginator('list_objects')
    for result in paginator.paginate(Bucket=bucket, Delimiter='/', Prefix=dist):
        if result.get('CommonPrefixes') is not None:
            for subdir in result.get('CommonPrefixes'):
                download_dir(client, resource, subdir.get('Prefix'), local, bucket)
        for file in result.get('Contents', []):
            dest_pathname = os.path.join(local, file.get('Key'))
            if not os.path.exists(os.path.dirname(dest_pathname)):
                os.makedirs(os.path.dirname(dest_pathname))
            resource.meta.client.download_file(bucket, file.get('Key'), dest_pathname)

The function is called that way:

Try this code with specifying your values. This will work well with your systems.

def _start():
    client = boto3.client('s3')
    resource = boto3.resource('s3')
    download_dir(client, resource, 'clientconf/', '/tmp', bucket='my-bucket')

Related Solutions

PLEASE ANSWER ONLY IF YOU KNOW subject is Unix System programming Create a SQLite Replit(or SQLite...
PLEASE ANSWER ONLY IF YOU KNOW subject is Unix System programming Create a SQLite Replit(or SQLite on another coding platform) that creates a table representing students in a class, including   name, graduation year, major, and GPA. Include two queries searching this student data in different ways, for example searching for all students with graduation year of 2021, or all students with GPA above a certain number.
Can someone please write clear and concise comments explaining what each line of code is doing...
Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks #include <stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> int join(char *com1[], char *com2[]) {    int p[2], status;    switch (fork()) {        case -1:            perror("1st fork call in join");            exit(3);        case 0:            break;        default:...
Explain how the interrupt mechanism works in Unix? Use a clear and concise diagram.
Explain how the interrupt mechanism works in Unix? Use a clear and concise diagram.
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just need help understanding the code, don't need to modify it. The purpose of the code is to count the frequency of words in a text file, and return the most frequent word with its count. It uses two algorithms: Algorithm 1 is based on the data structure LinkedList. It maintains a list for word frequencies. The algorithm runs by scanning every token in the...
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”....
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”. It takes one or more files and displays requested number of lines from the ending. If number is not specified, then it print 10 lines by default.
II. ***Write the following information in a more clear and concise manner, as it would appear...
II. ***Write the following information in a more clear and concise manner, as it would appear in the medical record. Include an appropriate subheading. 1. The patient walked 75 feet in the hallway of the hospital with the therapist lightly touching her back. She used a front-wheeled walker. The therapist was needed to help provide the patient with support to maintain balance. 2. The patient’s strength was 3/5 for the right biceps and 4/5 for the right triceps. 3. Upon...
Econometrics: Can someone please give a clear, concise and intuitive explanation of the rank of a...
Econometrics: Can someone please give a clear, concise and intuitive explanation of the rank of a matrix and how to find the rank using examples. WITHOUT REFERENCE TO ECHELON FORM.
write a half page clear and concise summary of the article and what you learned new...
write a half page clear and concise summary of the article and what you learned new and gleaned on the article. The compliance revolution after the passage of the Sarbanes-Oxley Act of 2002 (SOX) was accomplished in large part with the help of the internal control framework of the Committee of Sponsoring Organizations of the Treadway Commission (COSO). COSO’s framework became part of a worldwide movement to enhance periodic accounting and reporting of financial results. Coupled with the global convergence...
Please be clear, concise and up to the point in your answer. Give real world examples...
Please be clear, concise and up to the point in your answer. Give real world examples to support your arguments. West Coast Unlimited is a wholesaler that carries close to 20,000 products. The company has almost 3,000 suppliers and sells its products mostly to business and institutional customers. The company markets its products by relying mainly on sales promotion and advertising. Faced with increasing costs, the company is looking at various ways to reduce expenses. West Coast Unlimited's vice president...
Please write with structure, not class! Add comments for clear understanding. Write code without using scope...
Please write with structure, not class! Add comments for clear understanding. Write code without using scope resolution operators. Write an ADT to represent a doctor's medical registration number (a non-negative integer), number of patients (a non-negative integer), and income (non-negative, in euros). A doctor's information should be represented by a structure with 3 fields: medical_reg and num_patients of type int, and income of type float. Your ADT should define four interface functions to carry out the following operations: · Construct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT