How to load or display content of the file from AWS S3

조회 수: 31 (최근 30일)
Mukesh Dangi
Mukesh Dangi 2018년 6월 29일
답변: Mukesh Dangi 2018년 7월 3일
I have following code which load files from AWS s3 however i'm not sure how to display os access the these files one by one: -
%Set S3 Credentials
setenv('AWS_ACCESS_KEY_ID', '{ID}');
setenv('AWS_SECRET_ACCESS_KEY','{KEY}');
setenv('AWS_REGION', 'us-east-1');
%Load Data
imds = fileDatastore('s3://{bucket}/',...
'FileExtensions',{'.txt', '.pdf'},...
'ReadFcn',@load,...
'IncludeSubfolders',true);
file=imds;
disp(file);
output
FileDatastore with properties:
Files: {
's3://{bucket}/2017%20EBA%20-%20Relets%20(part%201)%20-%20signed.pdf'
's3://{bucket}/AQS94%20-%20Official%20Website.pdf'
's3://{bucket}/summary.txt'
}
UniformRead: 0
ReadFcn: @load
AlternateFileSystemRoots: {}
I tried
file=imds.Files;
disp(file);
However it's not displaying the content of the file.

채택된 답변

Mukesh Dangi
Mukesh Dangi 2018년 7월 3일
Finally. Well following worked :
fileName = strrep(fileName,"s3://","https://s3.amazonaws.com/");
%fileName ='https://s3.amazonaws.com/{bucket}/summary.txt';
options = weboptions('ContentType','text');
data = webread(fileName, options);

추가 답변 (1개)

Hatem Helal
Hatem Helal 2018년 7월 2일
You need to call read on the datastore. Its hard to make a specific recommendation, but if your goal is to open the txt and pdf files in your S3 bucket locally: you could try using the open command instead of load as the ReadFcn of your fileDatastore. Something like the following
fds = fileDatastore('s3://{bucket}/',...
'FileExtensions',{'.txt', '.pdf'},...
'ReadFcn',@open,...
'IncludeSubfolders',true);
while hasdata(fds)
read(fds)
end
  댓글 수: 1
Mukesh Dangi
Mukesh Dangi 2018년 7월 2일
편집: Mukesh Dangi 2018년 7월 2일
Thank You for the suggestion. Unfortunately due to some reasons hasdata() is giving an error stating the provide valid input. I'm not sure whether i should use fileDataStore or not. My in objective is just to get the files from S3 and play{/r/w} with those files. The problem with my code is that it's giving me following output and i'm unable to get the file individually from it. I'm not sure whether it's list
Files: {
's3://{bucket}/2017%20EBA%20-%20Relets%20(part%201)%20-%20signed.pdf'
's3://{bucket}/AQS94%20-%20Official%20Website.pdf'
's3://{bucket}/summary.txt'
}
UniformRead: 0
ReadFcn: @load
AlternateFileSystemRoots: {}
Moreover My code is able to fetch the files however unable to display desired results which is content of the file.
function handle()
%Set S3 Credentials
setenv('AWS_ACCESS_KEY_ID', '{ID}');
setenv('AWS_SECRET_ACCESS_KEY','{KEY}');
setenv('AWS_REGION', 'us-east-1');
%Load Data
imds = fileDatastore('s3://{buket}/',...
'FileExtensions',{'.txt'},...
'ReadFcn',@load,...
'IncludeSubfolders',true);
data=imds.Files{1};
AWSRead(data);
end
function AWSRead(fileName)
fileID = fopen(fileName,'r');
txt= textscan(fileID, '%s');
fclose(fileID);
whos txt
end
Error : Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by