difference between client and resource in boto3

+1 vote
Feb 28, 2019 in AWS by anonymous
5,416 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
• 57,700 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,150 points
741 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,320 points
1,055 views
+3 votes
10 answers

Difference between s3n, s3a and s3?

S3 Native FileSystem (URI scheme: s3n) A ...READ MORE

answered Oct 25, 2018 in AWS by Rishab rohan
20,499 views
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,456 views
0 votes
0 answers
0 votes
1 answer