SlideShare a Scribd company logo
Filesystem Management
with Flysystem
Mark Niebergall
https://joind.in/talk/63bca
Filesystem Management with Flysystem - php[tek] 2023
👏 Thank you!
• Organizers
• Attendees
• Speakers
• Sponsors
- Nucleus Security
🧑💻 PHP Community
• You are amazing
• Attending a conference
- Learn
- Network
- Have fun
- Apply what you learned
• 🙋 Poll: Who is already using Flysystem?
Filesystem Management with Flysystem - php[tek] 2023
https://img
fl
ip.com/i/779qzd
🤔 Question
• How do you e
ff
ectively manage
fi
les with code?
🤔 Question
• Common solution
- fopen
-
fi
le_get_contents
-
fi
le_put_contents
- delete
- chmod
https://img
fl
ip.com/i/778h2c
AwsS3Service
Local
In Memory
AWS S3
Azure
SFTP
File
LocalService
InMemoryService
AzureService
SftpService
😵💫 The Problem
• “Flysystem is a
fi
le storage library for PHP. It provides one
interface to interact with many di
ff
erent types of
fi
lesystems. When you use Flysystem, you’re not only
protected from vendor lock-in, you’ll also have a
consistent experience for which ever storage is right for
you.”
Flysystem
Local
In Memory
AWS S3
Azure
SFTP
File
✅ Objective
• Familiarity with Flysystem API
• Ideas on implementation
• Improved
fi
lesystem interactions
📋 Overview
• Adapters
• Flysystem API
• Implementation Strategies
• Testing
Adapters
https://gifer.com/en/EgDb
Adapters
• Traditional
- Di
ff
erent code for local vs external
- Di
ff
erent code for each adapter
Adapters
• Use case
- Local for development
- SFTP for QA
- S3 storage for Production
Adapters
• Use case
- Variety of
fi
le types
Adapters
public function readLocal(string $location): string
{
return file_get_contents($location);
}
public function readS3(string $location): string
{
// read file from S3 storage
return 'File Contents';
}
public function getContents(string $storage, string $location): string
{
return match ($storage) {
'local' => $this->readLocal($location),
'awss3' => $this->readS3($location),
default => throw new InvalidArgumentException('Not configured'),
};
}
Adapters
• Flysystem is con
fi
gurable
- Local
- External services
Adapters
• O
ffi
cial Adapters - Local
- Local
- InMemory
Adapters
• O
ffi
cial Adapters - External
- AWS S3
- AsyncAws S3
- Azure Blob Storage
- Google Cloud Storage
- SFTP
- WebDAV
Adapters
• Community Adapters
- GitLab
Adapters
• Decorators
- Read-Only
‣ $adapter = new
InMemoryFilesystemAdapter();
$readOnlyAdapter = new
LeagueFlysystemReadOnlyReadOnlyFile
systemAdapter($adapter);
$filesystem = new Filesystem($adapter);
Adapters
• Decorators
- Path Pre
fi
xing
‣ $pathPrefixedAdapter = new
LeagueFlysystemPathPrefixngPathPref
ixedAdapter($adapter, 'a/path/prefix');
Flysystem API
http://www.quickmeme.com/meme/3uywoc
Flysystem API
• Decent documentation online
- https://
fl
ysystem.thephpleague.com/docs/
- Make sure you are on the correction version of
documentation!
Flysystem API
• Exists
- fileExists(string $location): bool
- directoryExists(string $location): bool
- has(string $location): bool
‣ $this->adapter->fileExists(
$path
)
|| $this->adapter->directoryExists(
$path
);
Flysystem API
• File CRUD
- C: Write
- R: Read
- U: Move
- D: Delete
Flysystem API
• Write
- write(
string $location,
string $contents,
array $config = []
): void
- writeStream(
string $location,
$contents,
array $config = []
): void
Flysystem API
• Read
- read(string $location): string
- readStream(string $location)
Flysystem API
• Delete
- delete(string $location): void
- deleteDirectory(string $location): void
Flysystem API
• Directory
- createDirectory(
string $location,
array $config = []
): void
- listContents(
string $location,
bool $deep = self::LIST_SHALLOW
): DirectoryListing
Flysystem API
• Files
- move(
string $source,
string $destination,
array $config = []
): void
- copy(
string $source,
string $destination,
array $config = []
): void
Flysystem API
• Metadata
- lastModified(string $path): int
- fileSize(string $path): int
- mimeType(string $path): string
- checksum(
string $path,
array $config = []
): string
Flysystem API
• Permissions
- setVisibility(
string $path,
string $visibility
): void
- visibility(string $path): string
Flysystem API
• URLs
- S3, Azure, Google Cloud, WebDAV
‣ publicUrl(
string $path,
array $config = []
): string
‣ temporaryUrl(
string $path,
DateTimeInterface $expiresAt,
array $config = []
): string
Flysystem API
• Frameworks
- Bundled with Laravel
- Available with Symfony
- Can be used with other frameworks too
Implementation Strategies
https://makeameme.org/meme/and-then-we-5b7d94
Implementation Strategies
• Con
fi
guration
• Exception Handling
• Wrapper
• Mount Manager
Implementation Strategies
• Con
fi
guration
- Injectable con
fi
guration for adapters
'filesystem' => [
'local' => [
'directory' => '/tmp/filesystem/files/',
],
'awss3' => [
'client' => [
'credentials' => [
'key' => 'key...',
'secret' => 'secret...',
],
'region' => 'us‑west‑1',
'version' => 'latest',
],
'bucket' => 'my-bucket-name',
],
],
Implementation Strategies
• Con
fi
guration
- Application
fl
exibility
- Variety of sources
‣ Database
‣ PHP con
fi
guration
fi
les
Implementation Strategies
• Con
fi
guration
- UI interface to con
fi
gure
- Scalability
Implementation Strategies
• Exception Handling
- Generic exception
‣ FilesystemException
- Speci
fi
c exceptions
‣ UnableToReadFile
‣ UnableToWriteFile
‣ UnableToDeleteFile
‣ UnableToCheckExistence
‣ ...
Implementation Strategies
• Exception Handling
- Full list of exceptions
‣ https://
fl
ysystem.thephpleague.com/docs/usage/
fi
lesystem-api/
Implementation Strategies
• Exception Handling
1. Catch Flysystem speci
fi
c exceptions
2. Handle the exception
3. Throw own meaningful exception
Implementation Strategies
public function read(string $location): string
{
try {
$contents = $this->filesystem->read($location);
} catch (UnableToReadFile $exception) {
// handle unable to read exception
// throw an exception
} catch (FilesystemException $exception) {
// handle general error
// throw an exception
}
return $contents;
}
Implementation Strategies
• Wrapper
- Wrapper or Facade Pattern
- Abstracts away underlying package
- Swappable package
- Unit testable
Implementation Strategies
• Wrapper
- Useful pattern for many services
‣ Filesystem
‣ Email
‣ SMS
‣ Database
‣ Queue system
Implementation Strategies
class FileStorageService
{
public function __construct(
protected Filesystem $filesystem
) {}
public function read(string $location): string
{
return $this->filesystem->read($location);
}
Implementation Strategies
• Mount Manager
- Scenario
‣ Retrieve
fi
les from an external service
‣ Write locally for processing
Implementation Strategies
• Mount Manager
- Scenario
‣ Move
fi
les between external adapters upon an event
Implementation Strategies
• Mount Manager
- Scenario
‣ Many customers
‣ Reports created, stored locally
‣ Reports delivered based on con
fi
guration
‣ Variety of
fi
le storage locations
Implementation Strategies
• Mount Manager
- Interact with
fi
les in many storage services
- Specify adapter in location
‣ awss3://subfolder/
fi
le.txt
‣ local://path/to/
fi
le.txt
MountManager
Local
In Memory
AWS S3
Azure
SFTP
Implementation Strategies
Implementation Strategies
class FileManager
{
public function __construct(
protected MountManager $mountManager
) {}
public function read(string $location): string
{
return $this->mountManager->read($location);
}
Implementation Strategies
class FileManagerFactory
{
public function __construct(
protected FileStorageAdapterFactory $adapterFactory
) {}
public function create(array $mounts): FileManager
{
$mountManageConfig = [];
foreach ($mounts as $storage => $config) {
$mountManageConfig[$storage] = new Filesystem(
$this->adapterFactory->create($storage, $config)
);
}
return new FileManager(new MountManager($mountManageConfig));
}
Implementation Strategies
class FileStorageAdapterFactory
{
public function create(string $adapter, array $config): FilesystemAdapter
{
return match ($adapter) {
'awss3' => new AwsS3V3Adapter($config['client'], $config['bucket']),
'local' => new LocalFilesystemAdapter($config['directory']),
};
}
Testing
https://img
fl
ip.com/i/7ami3u
Testing
• Historically problematic testing
fi
le interactions
- Test
fi
les
- Inject
fi
le content
- git ignore directory for testing
fi
les
- Skip testing that code
- Integration tests vs Unit tests
Testing
public function testRead(): void
{
$file = __DIR__ . '/TestingFile.txt';
$service = new FileStorageLocalLegacyService();
$contents = $service->read($file);
$this->assertSame(
'Test file contents 123 ...',
$contents
);
}
Testing
• Flysystem abstraction layer allows for testing
- Mock calls to
fi
le interactions
- Pushes for single purpose code
- Centralized
fi
lesystem management
Testing
public function testRead(): void
{
$text = 'Test text!';
$testPath = uniqid('/tmp/test/') . '.txt';
$filesystemMock = $this->getMockBuilder(Filesystem::class)
->disableOriginalConstructor()
->onlyMethods(['read'])
->getMock();
$filesystemMock->method('read')
->with($testPath)
->willReturn($text);
$service = new FileStorageService($filesystemMock);
$contents = $service->read($testPath);
$this->assertSame($text, $contents);
}
🗣 Discussion
🗣 Discussion
• Bene
fi
ts of Flysystem?
• Cons of Flysystem?
🗣 Discussion
• Security
• Credentials
🗣 Discussion
• Conversion cost
• Scalability
• Maintenance
📋 Review
• External Services
• Flysystem API
• Implementation Strategies
• Testing
Mark Niebergall @mbniebergall
• PHP since 2005
• Masters degree in MIS
• Senior Software Engineer
• Vulnerability Management project (security scans)
• Utah PHP Co-Organizer
• CSSLP, SSCP Certi
fi
ed and Exam Developer
• Endurance sports, outdoors
Mark Niebergall @mbniebergall
Filesystem Management with Flysystem
• Questions?
• https://joind.in/talk/63bca
👀 References
• https://
fl
ysystem.thephpleague.com/docs/usage/
fi
lesystem-api/
• https://github.com/thephpleague/
fl
ysystem

More Related Content

PDF
Filesystem Management with Flysystem at PHP UK 2023
Mark Niebergall
 
PDF
Filesystems Lisbon 2018
Frank de Jonge
 
PDF
Filesystem Abstraction with Flysystem
Frank de Jonge
 
PDF
PyFilesystem
Andreas Jung
 
PDF
An Enhanced Cloud Backed Frugal File System
IRJET Journal
 
PPTX
Filesystem abstractions and msg queue sergeev - symfony camp 2018
Юлия Коваленко
 
PPTX
PHP deployment, 2016 flavor - cakefest 2016
Quentin Adam
 
PDF
Hybrid Cloud PHPUK2012
Combell NV
 
Filesystem Management with Flysystem at PHP UK 2023
Mark Niebergall
 
Filesystems Lisbon 2018
Frank de Jonge
 
Filesystem Abstraction with Flysystem
Frank de Jonge
 
PyFilesystem
Andreas Jung
 
An Enhanced Cloud Backed Frugal File System
IRJET Journal
 
Filesystem abstractions and msg queue sergeev - symfony camp 2018
Юлия Коваленко
 
PHP deployment, 2016 flavor - cakefest 2016
Quentin Adam
 
Hybrid Cloud PHPUK2012
Combell NV
 

Similar to Filesystem Management with Flysystem - php[tek] 2023 (20)

KEY
Anatomy of a high-volume, cloud-based WordPress architecture
Gabriel Koen
 
PDF
Quixote
ceiparua
 
PDF
Hopping in clouds - phpuk 17
Michele Orselli
 
PDF
Develop and deploy using Hybrid Cloud Strategies confoo2012
Combell NV
 
PPTX
Spring Boot & Spring Cloud on k8s and PCF
Lars Rosenquist
 
PDF
Inception Pack Vol 2: Bizarre premium
The Planning Lab
 
PDF
OSDC 2010 | Use Distributed Filesystem as a Storage Tier by Fabrizio Manfred
NETWAYS
 
PDF
Scaling PHP apps
Matteo Moretti
 
PDF
Microservices Runtimes
Frank Munz
 
PDF
Midwest php 2013 deploying php on paas- why & how
dotCloud
 
PDF
The Patterns of Distributed Logging and Containers
SATOSHI TAGOMORI
 
PDF
Disk to Cloud: Abstract your File Operations with CBFS
Ortus Solutions, Corp
 
PPTX
Cloud Foundry Roadmap in 2016
Cloud Standards Customer Council
 
PDF
Using amazon web services with cold fusion 11
ColdFusionConference
 
PPT
Distributed File Systems
awesomesos
 
PPTX
File Context
Hadoop User Group
 
PPTX
Microservices and Best Practices
Weaveworks
 
PPTX
EPrints and the Cloud
Leslie Carr
 
PDF
Amazon EFS (Elastic File System) 이해하고사용하기
Amazon Web Services Korea
 
PPTX
Dev & Test on AWS - Hebrew Webinar
Boaz Ziniman
 
Anatomy of a high-volume, cloud-based WordPress architecture
Gabriel Koen
 
Quixote
ceiparua
 
Hopping in clouds - phpuk 17
Michele Orselli
 
Develop and deploy using Hybrid Cloud Strategies confoo2012
Combell NV
 
Spring Boot & Spring Cloud on k8s and PCF
Lars Rosenquist
 
Inception Pack Vol 2: Bizarre premium
The Planning Lab
 
OSDC 2010 | Use Distributed Filesystem as a Storage Tier by Fabrizio Manfred
NETWAYS
 
Scaling PHP apps
Matteo Moretti
 
Microservices Runtimes
Frank Munz
 
Midwest php 2013 deploying php on paas- why & how
dotCloud
 
The Patterns of Distributed Logging and Containers
SATOSHI TAGOMORI
 
Disk to Cloud: Abstract your File Operations with CBFS
Ortus Solutions, Corp
 
Cloud Foundry Roadmap in 2016
Cloud Standards Customer Council
 
Using amazon web services with cold fusion 11
ColdFusionConference
 
Distributed File Systems
awesomesos
 
File Context
Hadoop User Group
 
Microservices and Best Practices
Weaveworks
 
EPrints and the Cloud
Leslie Carr
 
Amazon EFS (Elastic File System) 이해하고사용하기
Amazon Web Services Korea
 
Dev & Test on AWS - Hebrew Webinar
Boaz Ziniman
 
Ad

More from Mark Niebergall (20)

PDF
Leveling Up With Unit Testing - php[tek] 2023
Mark Niebergall
 
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
PDF
Developing SOLID Code
Mark Niebergall
 
PDF
Unit Testing from Setup to Deployment
Mark Niebergall
 
PDF
Stacking Up Middleware
Mark Niebergall
 
PDF
BDD API Tests with Gherkin and Behat
Mark Niebergall
 
PDF
BDD API Tests with Gherkin and Behat
Mark Niebergall
 
PDF
Hacking with PHP
Mark Niebergall
 
PDF
Relational Database Design Bootcamp
Mark Niebergall
 
PDF
Starting Out With PHP
Mark Niebergall
 
PDF
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
Mark Niebergall
 
PDF
Debugging PHP with Xdebug - PHPUK 2018
Mark Niebergall
 
PDF
Advanced PHP Simplified - Sunshine PHP 2018
Mark Niebergall
 
PDF
Defensive Coding Crash Course Tutorial
Mark Niebergall
 
PDF
Inheritance: Vertical or Horizontal
Mark Niebergall
 
PDF
Cybersecurity State of the Union
Mark Niebergall
 
PDF
Cryptography With PHP - ZendCon 2017 Workshop
Mark Niebergall
 
PDF
Defensive Coding Crash Course - ZendCon 2017
Mark Niebergall
 
PDF
Leveraging Composer in Existing Projects
Mark Niebergall
 
PDF
Defensive Coding Crash Course
Mark Niebergall
 
Leveling Up With Unit Testing - php[tek] 2023
Mark Niebergall
 
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
Developing SOLID Code
Mark Niebergall
 
Unit Testing from Setup to Deployment
Mark Niebergall
 
Stacking Up Middleware
Mark Niebergall
 
BDD API Tests with Gherkin and Behat
Mark Niebergall
 
BDD API Tests with Gherkin and Behat
Mark Niebergall
 
Hacking with PHP
Mark Niebergall
 
Relational Database Design Bootcamp
Mark Niebergall
 
Starting Out With PHP
Mark Niebergall
 
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
Mark Niebergall
 
Debugging PHP with Xdebug - PHPUK 2018
Mark Niebergall
 
Advanced PHP Simplified - Sunshine PHP 2018
Mark Niebergall
 
Defensive Coding Crash Course Tutorial
Mark Niebergall
 
Inheritance: Vertical or Horizontal
Mark Niebergall
 
Cybersecurity State of the Union
Mark Niebergall
 
Cryptography With PHP - ZendCon 2017 Workshop
Mark Niebergall
 
Defensive Coding Crash Course - ZendCon 2017
Mark Niebergall
 
Leveraging Composer in Existing Projects
Mark Niebergall
 
Defensive Coding Crash Course
Mark Niebergall
 
Ad

Recently uploaded (20)

PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PPTX
Smart Panchayat Raj e-Governance App.pptx
Rohitnikam33
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Presentation about variables and constant.pptx
safalsingh810
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Smart Panchayat Raj e-Governance App.pptx
Rohitnikam33
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 

Filesystem Management with Flysystem - php[tek] 2023