difference between client and resource in boto3

+1 vote
Mar 1, 2019 in AWS by anonymous
10,291 views

1 answer to this question.

+1 vote

The main difference between Client and Resources are as follows:

Client Resource
  • low-level service access
  • generated from service description
  • exposes botocore client to the developer
  • typically maps 1:1 with the service API
  • higher-level, object-oriented API
  • generated from resource description
  • uses identifiers and attributes
  • has actions (operations on resources)
  • exposes subresources and collections

answered Mar 1, 2019 by Priyaj
• 58,020 points
Please give some examples to understand the difference better.

Here's an example of using boto client-level access to an s3 bucket:

import boto3

    client = boto3.client('s3')
    response = client.list_objects(Bucket='example')
    for content in response['cont']:
        obj_dict = client.get_object(Bucket='example', Key=cont['Key'])
        print(cont['Key'], obj_dict['LastModified'])

an example of using boto resource-level access to an s3 bucket:

import boto3

    s3 = boto3.resource('s3')
    bucket = s3.Bucket('example')
    for obj in bucket.objects.all():
        print(obj.key, obj.last_modified)

Error in copy-pasted code:

import boto3
    client = boto3.client('s3')
    response = client.list_objects(Bucket='example')
    for content in response['cont']:
        obj_dict = client.get_object(Bucket='example', Key=cont['Key'])
        print(cont['Key'], obj_dict['LastModified'])

Where is "cont" defined for Key=cont['Key'] ?

It can't be a typo for the actual working code?

It's can happen only in copy-paste code 

This is just a part of a program. You can customize your code accordingly.

Related Questions In AWS

0 votes
1 answer

What is difference between commands and container commands in Elastic Beanstalk?

The major difference between these two pieces ...READ MORE

answered Sep 12, 2018 in AWS by Archana
• 4,170 points
2,519 views
+1 vote
1 answer

What is the difference between an Instance, AMI and Snaphots in AWS?

AMI is the Amazon Machine Image which provides ...READ MORE

answered May 24, 2019 in AWS by ArchanaNagur
• 2,360 points
4,216 views
0 votes
0 answers
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP