フィルターのクリア

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

27 ビュー (過去 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 Center および File ExchangeFile Operations についてさらに検索

タグ

タグが未入力です。

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by