1

Python scrub here. Im still learning python so sorry. Im trying to create a Dict(i think) that then behaves as a variable called fileshare and then want to call each entry inside the variable called fileshareARN. So basically inside the AWS ARN I want each share to be called. for example I want share-A, share-B, etc to be called each time. Im guessing I need to setup a function or a IF statement but im not sure.

import boto3

client = boto3.client('storagegateway')
fileshare = [share-A, share-B, share-C, share-D]

response = client.refresh_cache(
    FileShareARN='arn:aws:storagegateway:us-west-1:AWS-ID:share/{Fileshare-variable, share-ID should go here}.format',
    FolderList=['/'],
    Recursive=True
)

2 Answers 2

2

You're very close! A few notes to preface the answer to assist you on your Python journey:

  • Python does not allow hyphenated variable names, as hyphens are a reserved operator for subtraction. You only had them listed as placeholders, but figured it would be helpful to know.

  • lists, arrays, and dictionaries are all different data structures in Python. You can read about them more here: https://docs.python.org/3/tutorial/datastructures.html , but for your particular use case, if you're simply trying to store a collection of variables and iterate over them, a list or array work fine (although a dictionary is usable as well).


In Python, lists and arrays are iterables, which means that they have built-in functions that can naturally be iterated over to sequentially access their constituent values.

Let's go over an example using the following array: fruits = ['apples','bananas','oranges'],

In other languages, you're probably used to having to define your own loop with the following syntax:

for (int i = 0; i < sizeOf(fruits); i++)
{
   print(fruits[i]);
}

Python enables this same functionality much more easily.

for item in fruits:
   print(item)

Here, the scope of the term item within the loop is equal to the value that exists at that current index in the array (fruits).

Now, to perform this same functionality for your example, we can use this same technique to loop over your list of ARNs:

import boto3

client = boto3.client('storagegateway')
fileshare = [shareA, shareB, shareC, shareD]

for path in fileshare:
    response = client.refresh_cache(
        FileShareARN='arn:aws:storagegateway:us-west-1:AWS-ID:share/'+path,
        FolderList=['/'],
        Recursive=True
    )

After changing the placeholder variables you had in fileshare, I wrapped the existing response variable execution with a for loop, and made a slight change to the string appending at the end of your FileShareARN variable.

Hope this helps, and welcome to Python!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your input! Sorry for delayed response just got back from vacay.
0

Did some more research, found f.string formatting which seems to make python life easy. Also since I am deploying this in AWS Lambda I added a handler.

#!/usr/bin/env python3

import boto3
def default_handler( event, context ):
    print(boto3.client('sts').get_caller_identity())

    client = boto3.client('storagegateway')
    fileshare = ['share-A', 'share-B', 'share-C', 'share-D']

for path in fileshare:
    response = client.refresh_cache(
        FileShareARN = f"arn:aws:storagegateway:us-west-1:ARN-ID:share/{path}",
        FolderList=['/'],
        Recursive=True
    )
    print(response)

default_handler( None, None )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.