Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
s3.create_bucket("MyBucket")

If you're interested in streaming netCDF files directly from S3 buckets, give this example a look:

Code Block
languagepy
import netCDF4 as nc
import os
import glob
import xarray as xr
import boto3 
from botocore.session import Session
from botocore.handlers import validate_bucket_name
import tempfile


def load_S3():
    project_id = 'FILL ME IN' 
    bucketname = 'FILL ME IN'   
    access_key = 'FILL ME IN' 
    secret_access_key = 'FILL ME IN' 
    endpoint = 'https://s3.waw3-1.cloudferro.com'  
    bucketname = project_id + ':' + bucketname
    botocore_session = Session()
    botocore_session.unregister('before-parameter-build.s3', validate_bucket_name)
    boto3.setup_default_session(botocore_session = botocore_session)
    s3 = boto3.client('s3', endpoint_url=endpoint,
            aws_access_key_id = access_key,
            aws_secret_access_key = secret_access_key)
    files = []
    #List the objects in our bucket
    response = s3.list_objects(Bucket=bucketname)
    for item in response['Contents']:
        files.append(item["Key"])
    return s3, files, bucketname

def load_s3_file(filename):
    s3, files, bucketname = load_S3()
    tmp = tempfile.NamedTemporaryFile()
    with open(tmp.name, 'wb') as f:
        s3.download_fileobj(bucketname, filename, f)
        dataSet = xr.open_dataset(tmp.name, engine='netcdf4')
    return dataSet

def main():
    netCDFfile = load_s3_file("myfile.nc")

main()


If you're interested in more, I recommend taking a look at this article, which gives you a more detailed view into boto3's functionality (although it does emphasize on Amazon Web Services specifically, you can take a look at the Python code involved):

...