1
1
import os
2
2
import s3fs
3
3
import pathlib
4
- from tempfile import NamedTemporaryFile
5
- import logging
6
4
import boto3
7
5
import moto
8
6
import pyfive
9
7
import pytest
10
- from botocore .exceptions import ClientError
8
+ import h5netcdf
9
+
10
+ from tempfile import NamedTemporaryFile
11
11
12
12
13
- def file_upload (upload_file_bucket , file_name , file_path ):
14
- if os .path .exists (file_path ):
15
- with open (file_path , 'r' ) as f :
16
- xml = f .read ()
17
- else :
18
- logging .error ("File '%s' does not exist." % file_path )
19
- tools .exit_gracefully (botocore .log )
20
- try :
13
+ def spoof_s3 (bucket , file_name , file_path ):
14
+ # "put" file
15
+ if os .path .exists (file_path ):
16
+ with open (file_path , "rb" ) as file_contents :
21
17
conn = boto3 .session .Session ()
22
18
s3 = conn .resource ('s3' )
23
- object = s3 .Object (upload_file_bucket , file_name )
24
- result = object .put (Body = xml )
19
+ object = s3 .Object (bucket , file_name )
20
+ result = object .put (Body = file_contents )
25
21
res = result .get ('ResponseMetadata' )
26
22
if res .get ('HTTPStatusCode' ) == 200 :
27
- logging . info ('File Uploaded Successfully' )
23
+ print ('File Uploaded Successfully' )
28
24
else :
29
- logging .info ('File Not Uploaded Successfully' )
30
- return res
31
- except ClientError as e :
32
- logging .error (e )
25
+ print ('File Not Uploaded Successfully' )
33
26
27
+ # "download" file
28
+ s3 = boto3 .resource ('s3' )
29
+ # arg0: file in bucket; arg1: file to download to
30
+ target_file = "test.nc"
31
+ s3file = s3 .Bucket (bucket ).download_file (file_name , target_file )
32
+ print (os .path .isfile (target_file ))
34
33
35
- def file_load (bucket , file_name ):
36
- conn = boto3 .session .Session ()
37
- s3 = conn .resource ('s3' )
38
- object = s3 .Object (bucket , file_name )
39
- result = object .get (Range = "0=2" )
40
- print ("S3 Test mock file:" , result )
34
+ # "access" file "remotely" with s3fs
35
+ fs = s3fs .S3FileSystem ()
36
+ with open ('testobj.nc' , 'wb' ) as ncdata :
37
+ object .download_fileobj (ncdata )
38
+ with open ('testobj.nc' , 'rb' ) as ncdata :
39
+ ncfile = h5netcdf .File (ncdata , 'r' , invalid_netcdf = True )
40
+ print (ncfile ) # it works but...
41
41
42
- ds = pyfive .File (result )
43
- return ds
42
+ return res
44
43
45
44
46
45
@pytest .fixture (scope = 'session' )
@@ -54,8 +53,7 @@ def aws_credentials():
54
53
55
54
try :
56
55
tmp = NamedTemporaryFile (delete = False )
57
- # you many need to change 'aws_prof_dev_qa' to be your profile name
58
- tmp .write (b"""[aws_prof_dev_qa]
56
+ tmp .write (b"""[wild weasel]
59
57
aws_access_key_id = testing
60
58
aws_secret_access_key = testing""" )
61
59
tmp .close ()
@@ -77,12 +75,9 @@ def empty_bucket(aws_credentials):
77
75
moto_fake .stop ()
78
76
79
77
80
- def test_file_upload (empty_bucket ):
81
- with NamedTemporaryFile () as tmp :
82
- tmp .write (b'Hi' )
83
- file_name = pathlib .Path (tmp .name ).name
84
-
85
- result = file_upload ("MY_BUCKET" , file_name , tmp .name )
86
- result2 = file_load ("MY_BUCKET" , file_name )
87
-
88
- assert result .get ('HTTPStatusCode' ) == 200
78
+ def test_s3file_spoofing (empty_bucket ):
79
+ ncfile = "./tests/test_data/daily_data.nc"
80
+ file_path = pathlib .Path (ncfile )
81
+ file_name = pathlib .Path (ncfile ).name
82
+ result = spoof_s3 ("MY_BUCKET" , file_name , file_path )
83
+ assert result .get ('HTTPStatusCode' ) == 200
0 commit comments