SlideShare a Scribd company logo
1 of 47
Download to read offline
FILESYSTEM 
ABSTRACTION 
WITH FLYSYSTEM 
By @FrankDeJonge
WHAT IS FILESYSTEM ABSTRACTION? 
Bridging the gap between filesystems so the storage engine 
becomes an implementation detail.
FILESYSTEM 
ABSTRACTION 101 
What are filesystems? 
How do we use filesystems? 
Moving to remote filesystems. 
Abstraction saves the day.
WHAT IS A FILESYSTEM? 
LOGIC & STRUCTURE
WHAT IS A FILESYSTEM? 
Logic to structure and organize pieces of data in a storage 
system.
WHAT'S IN A FILESYSTEM? 
Files 
Directories 
Metadata
COMMON METADATA 
Location / Path 
Timestamps 
Permissions / Ownership
FILE SPECIFIC 
Mime-type 
File size 
Contents
DERIVED INFORMATION / PATHINFO(): 
Filename 
Basename 
Extension 
Dirname 
Mime-type
FILESYSTEM STRUCTURE
STRUCTURE TYPES: 
Nested or Linear 
 src/ 
 Filesystem.php 
 AdapterInterface.php 
 tests/ 
 Filesystem.php 
 AdapterInterface.php 
 src/Filesystem.php 
 src/AdapterInterface.php 
 tests/Filesystem.php 
 tests/AdapterInterface.php
NESTED FILESYSTEMS 
Are the most common 
Have directories 
which have files and more directories 
... which have more files and directories 
... which have more files and directories. 
We're really used to this structure.
LINEAR FILESYSTEM 
Are like key/value stores 
do have files, of course 
require a different approach 
don't really have directories.
DIRECTORIES ARE A MYTH
WHAT CAN WE DO WITH 
A FILESYSTEM? 
Several operations: 
write 
read 
update 
delete 
move / rename 
copy 
list 
inspect
HANDLING FILESYSTEMS WITH PHP, A WORLD 
OF HURT.
In vanilla PHP for local files: 
$result = file_put_contents($location, $contents); 
$contents = file_get_contents($location); 
unlink($location); 
$timestamp = mtime($location); 
$mimetype = mime_content_type($location); // DEPRECATED 
$mimetype = (new Finfo(FILEINFO_MIME_TYPE))->file($location);
LISTING FILES 
glob($location . '/*'); // ? 
scandir($location); // ? 
readdir(opendir($location)); // ? 
array_map('normalize_fileinfo', 
iterator_to_array(new DirectoryIterator($location))); // ? 
array_map('normalize_fileinfo', 
iterator_to_array(new RecursiveIteratorIterator( 
new RecursiveDirectoryIterator($location) 
))); // ?
Memory problems? Use streams: 
$resource = fopen($rLocation, 'r+'); 
$handle = fopen($location, 'w+'); 
while ( ! feof($resource)) { 
fwrite($handle, fread($resource, 1024), 1024); 
} 
$result = fclose($handle); 
fclose($resource);
WORKING WITH OTHER 
FILESYSTEMS. 
BECAUSE REASONS.
SOME REASONS WHY: 
We want to scale (horizontally). 
... so, we want stateless apps. 
We don't have to serve files ourselves. 
Share files across multiple application. 
We want to enable a nice experience to our users which was 
not possible otherwise.
SOME CONSEQUENCES 
We can't use built-in PHP function anymore. 
We'll have to start using API's 
We'll have to depend on third party code.
COMPARING INTEGRATIONS: WRITING FILES 
AWS/S3: 
$options = [ 
'Body' => $contents, 
'ContentType' => 'plain/text', 
'ContentLength' => mb_strlen($contents), 
]; 
$result = $awsClient->putObject($options); 
Dropbox: 
$result = $dropboxClient->uploadFileFromString( 
$location, 
$contents, 
WriteMode::add());
Rackspace: 
$result = $rackspaceClient->uploadObject($location, $contents); 
WebDAV: 
$result = $client->request('PUT', $location, $contents); 
FTP: 
$stream = tmpfile(); 
fwrite($stream, $contents); 
ftp_fput($connection, $destination, $stream, FTP_BINARY); 
fclose($stream);
DOWNSIDES 
Different API 
Support different kinds of input (string vs stream support) 
Different results for EVERY operation
CHOOSING A FILESYSTEM ... 
can create technical dept 
can create vendor lock-in 
require you to invest in them
SOLUTION? 
USE AN ABSTRACT 
FILESYSTEM.
WHAT IS AN ABSTRACT FILESYSTEM? 
Logic and structure that enable a generalized API to work with 
different filesystems.
WHAT DOES A ABSTRACT FILESYSTEM DO? 
It bridges the gap between different filesystem 
implementations/packages and normalizes responses.
INTRODUCING: 
FLYSYSTEM 
MANY FILESYSTEMS, ONE API
FLYSYSTEM IS: 
A filesystem abstraction layer 
Like a DBAL for files. 
Like illuminate/filesystem on steroids.
PACKED WITH SUPERPOWERS: 
Well tested code 
Easy API 
Cachable metadata 
Easy stream handling
FIRST LOOK AT FLYSYSTEM 
use LeagueFlysystemAdapterLocal; 
$adapter = new Local(__DIR__.'/path/to/dir'); 
$fs = new Filesystem($adapter); 
BASIC FS OPERATIONS: 
$fs->write($location, $contents); 
$fs->read($location); 
$fs->update($location, $contents); 
$fs->listContents($location, $recursive); 
$fs->delete($location); 
$fs->getTimestamp($location);
USING AWS S3 
use LeagueFlysystemAdapterAwsS3; 
$adapter = new AwsS3($client, 'bucket-name'); 
$fs = new Filesystem($adapter); 
BASIC FS OPERATIONS: 
$fs->write($location, $contents); 
$fs->read($location); 
$fs->update($location, $contents); 
$fs->listContents($location, $recursive); 
$fs->delete($location); 
$fs->getTimestamp($location);
USING FTP 
use LeagueFlysystemAdapterFtp; 
$adapter = new Ftp($ftpSettings); 
$fs = new Filesystem($adapter); 
BASIC FS OPERATIONS: 
$fs->write($location, $contents); 
$fs->read($location); 
$fs->update($location, $contents); 
$fs->listContents($location, $recursive); 
$fs->delete($location); 
$fs->getTimestamp($location);
MANY FILESYSTEMS 
ONE API
FLYSYSTEM AND STREAMS: 
$resource = $dropbox->readStream($location); 
$awsS3->writeStream($destination, $resource);
FLYSYSTEM IS A GATEWAY DRUG FOR... 
... DRY code 
... centralized problem domain handling 
... easily testable FS interactions 
... reduced technical dept 
... lower development costs 
... having less to learn.
TESTING FILESYSTEM INTERACTIONS 
USING FLYSYSTEM 
Becomes easier. 
Becomes more reliable. 
Might even become fun again.
DEPENDING ON FLYSYSTEM 
class Author 
{ 
public function __construct(FilesystemInterface $filesystem) 
{ 
$this->filesystem = $filesystem; 
} 
public function getBio() 
{ 
return $this->filesystem->read($this->getBioLocation()); 
} 
}
TESTING THE DEPENDANT CODE 
public function testGetBio() 
{ 
$expected = 'Some Bio'; 
$fsMock = Mockery::mock('LeagueFlysystemFilesystemInterface'); 
$fsMock->shouldReceive('read') 
->with('james-doe-bio.txt') 
->andReturn($expected); 
$autor = new Author($fsMock); 
$this->assertEquals($expected, $author->getBio()); 
} 
BONUS: this makes tests run super fast because you don't have 
to wait for blocking filesystem operations (slow).
IMPROVED STABILITY OF DEPENDANT CODE 
Class should have one reason to change 
... changing filesystems isn't one of them. 
Most code shouldn't have to care where files are stored. 
Flysystem enables classes to be unaware of this.
OPENING UP TO NEW POSSIBILITIES 
Using Dropbox for uploads 
Ease development by deferring filesystem choice. 
Painless pivots during application lifecycle. 
Nice to build a package on.
FLYSYSTEM INTEGRATIONS 
Laravel Integration: 
https://github.com/GrahamCampbell/Laravel-Flysystem 
Symfony Bundle: 
https://github.com/1up-lab/OneupFlysystemBundle 
Backup Manager: 
https://github.com/heybigname/backup-manager 
Cartalyst Media: 
http://cartalyst.com
CRAZY FLYSYSTEM ADAPTERS 
NOTE: some might be urban myths, I haven't seen them all. 
Flickr 
Youtube 
Reddit
THE LEAGUE OF EXTRAORDINARY PACKAGES 
HTTP://GITHUB.COM/THEPHPLEAGUE/FLYSYSTEM
THANKS FOR LISTENING! 
Find me on twitter @FrankDeJonge

More Related Content

What's hot

Etika Berinternet.pptx
Etika Berinternet.pptxEtika Berinternet.pptx
Etika Berinternet.pptxssuser8f3f7f
 
Presentasi cloud computing
Presentasi cloud computingPresentasi cloud computing
Presentasi cloud computingSunarty
 
Capitulo 6 vlsm y cidr
Capitulo 6 vlsm y cidrCapitulo 6 vlsm y cidr
Capitulo 6 vlsm y cidrTeleredUSM
 
Firewall Security Definition
Firewall Security DefinitionFirewall Security Definition
Firewall Security DefinitionPatten John
 
installation of VM and ubuntu.pptx
installation of VM and ubuntu.pptxinstallation of VM and ubuntu.pptx
installation of VM and ubuntu.pptxArchanaD30
 
Jaringan komputer dan internet BAB 5
Jaringan komputer dan internet BAB 5Jaringan komputer dan internet BAB 5
Jaringan komputer dan internet BAB 5annisa s
 
Makalah jaringan komputer
Makalah jaringan komputer Makalah jaringan komputer
Makalah jaringan komputer Aryana R
 
Disk Drives Interfaces
Disk Drives InterfacesDisk Drives Interfaces
Disk Drives InterfacesAmir Villas
 
Prosedur Instalasi Server Softswitch Berbasis SIP
Prosedur Instalasi Server Softswitch Berbasis SIPProsedur Instalasi Server Softswitch Berbasis SIP
Prosedur Instalasi Server Softswitch Berbasis SIPFanny Fayu Laksono
 
Alphorm.com Formation Forensic sur Android
Alphorm.com Formation Forensic sur AndroidAlphorm.com Formation Forensic sur Android
Alphorm.com Formation Forensic sur AndroidAlphorm
 
Basic Malware Analysis
Basic Malware AnalysisBasic Malware Analysis
Basic Malware AnalysisAlbert Hui
 
Memory forensics.pptx
Memory forensics.pptxMemory forensics.pptx
Memory forensics.pptx9905234521
 
Backup and recovery
Backup and recoveryBackup and recovery
Backup and recoverydhawal mehta
 
Modul Praktikum Instalasi Sistem Operasi
Modul Praktikum  Instalasi Sistem  Operasi Modul Praktikum  Instalasi Sistem  Operasi
Modul Praktikum Instalasi Sistem Operasi Dwi Ely Kurniawan
 

What's hot (20)

Sandboxing
SandboxingSandboxing
Sandboxing
 
Etika Berinternet.pptx
Etika Berinternet.pptxEtika Berinternet.pptx
Etika Berinternet.pptx
 
Arch Linux Cheat
Arch Linux CheatArch Linux Cheat
Arch Linux Cheat
 
Raid
RaidRaid
Raid
 
Presentasi cloud computing
Presentasi cloud computingPresentasi cloud computing
Presentasi cloud computing
 
Capitulo 6 vlsm y cidr
Capitulo 6 vlsm y cidrCapitulo 6 vlsm y cidr
Capitulo 6 vlsm y cidr
 
Firewall Security Definition
Firewall Security DefinitionFirewall Security Definition
Firewall Security Definition
 
Array pada PHP
Array pada PHPArray pada PHP
Array pada PHP
 
installation of VM and ubuntu.pptx
installation of VM and ubuntu.pptxinstallation of VM and ubuntu.pptx
installation of VM and ubuntu.pptx
 
Keamanan Jaringan Komputer
Keamanan Jaringan KomputerKeamanan Jaringan Komputer
Keamanan Jaringan Komputer
 
Jaringan komputer dan internet BAB 5
Jaringan komputer dan internet BAB 5Jaringan komputer dan internet BAB 5
Jaringan komputer dan internet BAB 5
 
Makalah jaringan komputer
Makalah jaringan komputer Makalah jaringan komputer
Makalah jaringan komputer
 
05 wireless
05 wireless05 wireless
05 wireless
 
Disk Drives Interfaces
Disk Drives InterfacesDisk Drives Interfaces
Disk Drives Interfaces
 
Prosedur Instalasi Server Softswitch Berbasis SIP
Prosedur Instalasi Server Softswitch Berbasis SIPProsedur Instalasi Server Softswitch Berbasis SIP
Prosedur Instalasi Server Softswitch Berbasis SIP
 
Alphorm.com Formation Forensic sur Android
Alphorm.com Formation Forensic sur AndroidAlphorm.com Formation Forensic sur Android
Alphorm.com Formation Forensic sur Android
 
Basic Malware Analysis
Basic Malware AnalysisBasic Malware Analysis
Basic Malware Analysis
 
Memory forensics.pptx
Memory forensics.pptxMemory forensics.pptx
Memory forensics.pptx
 
Backup and recovery
Backup and recoveryBackup and recovery
Backup and recovery
 
Modul Praktikum Instalasi Sistem Operasi
Modul Praktikum  Instalasi Sistem  Operasi Modul Praktikum  Instalasi Sistem  Operasi
Modul Praktikum Instalasi Sistem Operasi
 

Similar to Filesystem Abstraction with Flysystem

Filesystems Lisbon 2018
Filesystems Lisbon 2018Filesystems Lisbon 2018
Filesystems Lisbon 2018Frank de Jonge
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappersPositive Hack Days
 
Building File Systems with FUSE
Building File Systems with FUSEBuilding File Systems with FUSE
Building File Systems with FUSEelliando dias
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedBertrand Dunogier
 
Introducing FSter
Introducing FSterIntroducing FSter
Introducing FSteritsmesrl
 
Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023Mark Niebergall
 
Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023Mark Niebergall
 
Writing and using php streams and sockets
Writing and using php streams and socketsWriting and using php streams and sockets
Writing and using php streams and socketsElizabeth Smith
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking Sebastian Marek
 
vfsStream - a better approach for file system dependent tests
vfsStream - a better approach for file system dependent testsvfsStream - a better approach for file system dependent tests
vfsStream - a better approach for file system dependent testsFrank Kleine
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Fabien Potencier
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionNate Abele
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingTricode (part of Dept)
 
eZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisitedeZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisitedBertrand Dunogier
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiJérémy Derussé
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8Alexei Gorobets
 

Similar to Filesystem Abstraction with Flysystem (20)

Filesystems Lisbon 2018
Filesystems Lisbon 2018Filesystems Lisbon 2018
Filesystems Lisbon 2018
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Building File Systems with FUSE
Building File Systems with FUSEBuilding File Systems with FUSE
Building File Systems with FUSE
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster Unleashed
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
 
Introducing FSter
Introducing FSterIntroducing FSter
Introducing FSter
 
Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023
 
Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023
 
Writing and using php streams and sockets
Writing and using php streams and socketsWriting and using php streams and sockets
Writing and using php streams and sockets
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
 
vfsStream - a better approach for file system dependent tests
vfsStream - a better approach for file system dependent testsvfsStream - a better approach for file system dependent tests
vfsStream - a better approach for file system dependent tests
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching logging
 
eZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisitedeZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisited
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
Training on php by cyber security infotech (csi)
Training on  php by cyber security infotech (csi)Training on  php by cyber security infotech (csi)
Training on php by cyber security infotech (csi)
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 

Recently uploaded

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Recently uploaded (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

Filesystem Abstraction with Flysystem

  • 1. FILESYSTEM ABSTRACTION WITH FLYSYSTEM By @FrankDeJonge
  • 2. WHAT IS FILESYSTEM ABSTRACTION? Bridging the gap between filesystems so the storage engine becomes an implementation detail.
  • 3. FILESYSTEM ABSTRACTION 101 What are filesystems? How do we use filesystems? Moving to remote filesystems. Abstraction saves the day.
  • 4. WHAT IS A FILESYSTEM? LOGIC & STRUCTURE
  • 5. WHAT IS A FILESYSTEM? Logic to structure and organize pieces of data in a storage system.
  • 6. WHAT'S IN A FILESYSTEM? Files Directories Metadata
  • 7. COMMON METADATA Location / Path Timestamps Permissions / Ownership
  • 8. FILE SPECIFIC Mime-type File size Contents
  • 9. DERIVED INFORMATION / PATHINFO(): Filename Basename Extension Dirname Mime-type
  • 11. STRUCTURE TYPES: Nested or Linear  src/  Filesystem.php  AdapterInterface.php  tests/  Filesystem.php  AdapterInterface.php  src/Filesystem.php  src/AdapterInterface.php  tests/Filesystem.php  tests/AdapterInterface.php
  • 12. NESTED FILESYSTEMS Are the most common Have directories which have files and more directories ... which have more files and directories ... which have more files and directories. We're really used to this structure.
  • 13. LINEAR FILESYSTEM Are like key/value stores do have files, of course require a different approach don't really have directories.
  • 15. WHAT CAN WE DO WITH A FILESYSTEM? Several operations: write read update delete move / rename copy list inspect
  • 16. HANDLING FILESYSTEMS WITH PHP, A WORLD OF HURT.
  • 17. In vanilla PHP for local files: $result = file_put_contents($location, $contents); $contents = file_get_contents($location); unlink($location); $timestamp = mtime($location); $mimetype = mime_content_type($location); // DEPRECATED $mimetype = (new Finfo(FILEINFO_MIME_TYPE))->file($location);
  • 18. LISTING FILES glob($location . '/*'); // ? scandir($location); // ? readdir(opendir($location)); // ? array_map('normalize_fileinfo', iterator_to_array(new DirectoryIterator($location))); // ? array_map('normalize_fileinfo', iterator_to_array(new RecursiveIteratorIterator( new RecursiveDirectoryIterator($location) ))); // ?
  • 19. Memory problems? Use streams: $resource = fopen($rLocation, 'r+'); $handle = fopen($location, 'w+'); while ( ! feof($resource)) { fwrite($handle, fread($resource, 1024), 1024); } $result = fclose($handle); fclose($resource);
  • 20. WORKING WITH OTHER FILESYSTEMS. BECAUSE REASONS.
  • 21. SOME REASONS WHY: We want to scale (horizontally). ... so, we want stateless apps. We don't have to serve files ourselves. Share files across multiple application. We want to enable a nice experience to our users which was not possible otherwise.
  • 22. SOME CONSEQUENCES We can't use built-in PHP function anymore. We'll have to start using API's We'll have to depend on third party code.
  • 23. COMPARING INTEGRATIONS: WRITING FILES AWS/S3: $options = [ 'Body' => $contents, 'ContentType' => 'plain/text', 'ContentLength' => mb_strlen($contents), ]; $result = $awsClient->putObject($options); Dropbox: $result = $dropboxClient->uploadFileFromString( $location, $contents, WriteMode::add());
  • 24. Rackspace: $result = $rackspaceClient->uploadObject($location, $contents); WebDAV: $result = $client->request('PUT', $location, $contents); FTP: $stream = tmpfile(); fwrite($stream, $contents); ftp_fput($connection, $destination, $stream, FTP_BINARY); fclose($stream);
  • 25. DOWNSIDES Different API Support different kinds of input (string vs stream support) Different results for EVERY operation
  • 26. CHOOSING A FILESYSTEM ... can create technical dept can create vendor lock-in require you to invest in them
  • 27. SOLUTION? USE AN ABSTRACT FILESYSTEM.
  • 28. WHAT IS AN ABSTRACT FILESYSTEM? Logic and structure that enable a generalized API to work with different filesystems.
  • 29. WHAT DOES A ABSTRACT FILESYSTEM DO? It bridges the gap between different filesystem implementations/packages and normalizes responses.
  • 30. INTRODUCING: FLYSYSTEM MANY FILESYSTEMS, ONE API
  • 31. FLYSYSTEM IS: A filesystem abstraction layer Like a DBAL for files. Like illuminate/filesystem on steroids.
  • 32. PACKED WITH SUPERPOWERS: Well tested code Easy API Cachable metadata Easy stream handling
  • 33. FIRST LOOK AT FLYSYSTEM use LeagueFlysystemAdapterLocal; $adapter = new Local(__DIR__.'/path/to/dir'); $fs = new Filesystem($adapter); BASIC FS OPERATIONS: $fs->write($location, $contents); $fs->read($location); $fs->update($location, $contents); $fs->listContents($location, $recursive); $fs->delete($location); $fs->getTimestamp($location);
  • 34. USING AWS S3 use LeagueFlysystemAdapterAwsS3; $adapter = new AwsS3($client, 'bucket-name'); $fs = new Filesystem($adapter); BASIC FS OPERATIONS: $fs->write($location, $contents); $fs->read($location); $fs->update($location, $contents); $fs->listContents($location, $recursive); $fs->delete($location); $fs->getTimestamp($location);
  • 35. USING FTP use LeagueFlysystemAdapterFtp; $adapter = new Ftp($ftpSettings); $fs = new Filesystem($adapter); BASIC FS OPERATIONS: $fs->write($location, $contents); $fs->read($location); $fs->update($location, $contents); $fs->listContents($location, $recursive); $fs->delete($location); $fs->getTimestamp($location);
  • 37. FLYSYSTEM AND STREAMS: $resource = $dropbox->readStream($location); $awsS3->writeStream($destination, $resource);
  • 38. FLYSYSTEM IS A GATEWAY DRUG FOR... ... DRY code ... centralized problem domain handling ... easily testable FS interactions ... reduced technical dept ... lower development costs ... having less to learn.
  • 39. TESTING FILESYSTEM INTERACTIONS USING FLYSYSTEM Becomes easier. Becomes more reliable. Might even become fun again.
  • 40. DEPENDING ON FLYSYSTEM class Author { public function __construct(FilesystemInterface $filesystem) { $this->filesystem = $filesystem; } public function getBio() { return $this->filesystem->read($this->getBioLocation()); } }
  • 41. TESTING THE DEPENDANT CODE public function testGetBio() { $expected = 'Some Bio'; $fsMock = Mockery::mock('LeagueFlysystemFilesystemInterface'); $fsMock->shouldReceive('read') ->with('james-doe-bio.txt') ->andReturn($expected); $autor = new Author($fsMock); $this->assertEquals($expected, $author->getBio()); } BONUS: this makes tests run super fast because you don't have to wait for blocking filesystem operations (slow).
  • 42. IMPROVED STABILITY OF DEPENDANT CODE Class should have one reason to change ... changing filesystems isn't one of them. Most code shouldn't have to care where files are stored. Flysystem enables classes to be unaware of this.
  • 43. OPENING UP TO NEW POSSIBILITIES Using Dropbox for uploads Ease development by deferring filesystem choice. Painless pivots during application lifecycle. Nice to build a package on.
  • 44. FLYSYSTEM INTEGRATIONS Laravel Integration: https://github.com/GrahamCampbell/Laravel-Flysystem Symfony Bundle: https://github.com/1up-lab/OneupFlysystemBundle Backup Manager: https://github.com/heybigname/backup-manager Cartalyst Media: http://cartalyst.com
  • 45. CRAZY FLYSYSTEM ADAPTERS NOTE: some might be urban myths, I haven't seen them all. Flickr Youtube Reddit
  • 46. THE LEAGUE OF EXTRAORDINARY PACKAGES HTTP://GITHUB.COM/THEPHPLEAGUE/FLYSYSTEM
  • 47. THANKS FOR LISTENING! Find me on twitter @FrankDeJonge